留恋 nyoj 854
生活随笔
收集整理的這篇文章主要介紹了
留恋 nyoj 854
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
留戀
時間限制:1000?ms ?|? 內(nèi)存限制:65535?KB 難度:2 ? 描述大家都知道,高中的時候,座位基本都是固定的,但是對于視力不好卻又坐在后面的人是很不公平的。
念情的高中班主任安哥是個非常好的班主任,他為每個人著想,制定了一種的換位規(guī)則,每周執(zhí)行一次:
????? ? 每次都向右下角移動一個位置(即本周坐(0,1),則下周坐(1,2))
????? ? 若已移動到邊上,則返回頂部(即在行數(shù)為3的情況下,本周在(2,n),則下周在(0,n+1),列同)
現(xiàn)在念情想知道,如果教室有(n*m)個座位,他能不能把所有的座位都坐一次呢?(假設念情讀高中的時間無限)
接下來N行每行兩個整數(shù)n,m表示教室的座位
1<n<10000
1<m<10000
方法一:利用最小公因數(shù)
//最小公因數(shù) #include<iostream> using namespace std; int gcd(int a, int b) ; int main() {int t;cin >> t;while (t--) {int n, m;cin >> n >> m;if (gcd(n, m) == 1) {cout << "Possible" << endl;} else {cout << "Impossible" << endl;}} } int gcd(int a, int b) {if (a%b == 0) return b;return gcd(b,a%b); }
方法二:
分析圖:
?
?
#include<iostream> #include<cstring> using namespace std; const int N = 10005; int col[N], row[N]; int main() {int t;cin >> t;while (t--) {int n, m;cin >> n >> m;int i , j, x, y;i = j = x = y = 1;memset(col,0,sizeof(col));memset(row,0,sizeof(row));while (x <= n && y <= m) { if (n-i+1 < m-j+1) {j = j+ n-i+1; i = n+1; }//i 先到達底部 同時移動n-i+1 步 else if (n-i+1 > m-j+1) {i = i +m-j+1; j = m+1; }//j 先到達底部 else { i = n+1; j = m+1;}//i、j 同時到達底部 if (i > n) { //i 比n大時i必等于n+1 i = 1;if (j <= m) {if (col[j]) break;col[j] = 1;y++;} else {//i 比m大時i必等于m+1 既要向左轉 col[1] = 1;y++;}}if (j > m) {//i 比m大時i必等于m+1 j = 1;if (i <= n) {if (row[i]) break;row[i] = 1;x++;} else {//i 比n大時i必等于n+1 既要向上轉 row[1] = 1;x++;}}} if (x == n+1 && y == m+1) {//每一個座位都做遍了 cout << "Possible" << endl;} else {cout << "Impossible" << endl;}} }?
?轉載于:https://www.cnblogs.com/dream-it-possible/p/8525165.html
總結
以上是生活随笔為你收集整理的留恋 nyoj 854的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Python】学习笔记7-异常处理tr
- 下一篇: 面向对象 - 1.面向过程/2.面向对象