if n is 2: move twice -> even, odd
if n is odd: move twice -> odd, odd
if n is even: move three times -> even, odd, odd
總之可以很容易的構造出一個方法可以從 n 變成 n-2,僅此而已
//By momo #include<cstdio> #include<algorithm> using namespace std; int main(){ int n; scanf("%d", &n); if(n & 1){ int cnt = n, st = 1; for(int l = n; l > 1; l -= 2, st++){ printf("%d", cnt); cnt += 2; for(int i = 0; i <= l; i += 2) printf(" %d %d", st+i+n*(st-1), st+i+n*(st+l-2)); for(int i = 2; i < l-1; i += 2) printf(" %d %d", st+(st+i-1)*n, st+l-1+(st+i-1)*n); puts(""); printf("%d", cnt); cnt += 2; for(int i = 1; i < l; i += 2) printf(" %d %d", st+i+n*(st-1), st+i+n*(st+l-2)); for(int i = 1; i < l; i += 2) printf(" %d %d", st+(st+i-1)*n, st+l-1+(st+i-1)*n); puts(""); } } else{ int cnt = n+1, cnt2 = n, st = 1; for(int l = n; l > 2; l -= 2, st++){ printf("%d", cnt2); cnt2 += 2; for(int i = 0; i < l; i++) printf(" %d", (i&1)? st+n*(st+i-1) : st+l-1+n*(st+i-1)); puts(""); printf("%d", cnt); cnt += 2; for(int i = 1; i <l-1; i++) printf(" %d", (i&1)? st+i+n*(st+l-2) : st+i+n*(st-1)); for(int i = 0; i < l; i++) printf(" %d", (i&1)? st+l-1+n*(st+i-1) : st+n*(st+i-1)); puts(""); printf("%d", cnt); cnt += 2; for(int i = 1; i <l-1; i++) printf(" %d", (i&1)? st+i+n*(st-1) : st+i+n*(st+l-2)); puts(""); } printf("%d %d\n%d %d %d\n", cnt2, st+n*(st-1)+1, cnt, st+n*(st-1), st+1+n*st); } }
沒有留言:
張貼留言