要找到規律,畫一個k=6的解,就可以找到規律了。不過雖然找到了規律,卻還不知道為什麼規律是這樣。
CODE
#include <iostream> int main(){ int k, n; int block; //k-1 bool flag = false; while( scanf("%d", &k) != EOF ) { if( flag ) printf("\n"); else flag = true; n = k * k - k + 1; block = k - 1; int i, j, t, p; int line = 0; //count total lines printed int starter = 1; //first number of every line int count = 2; //print first k lines for(i=0; i<k; ++i, ++line){ printf("%d", starter); for(j=1; j<k; ++j, ++count){ printf(" %d", count); }//j: k marks printf("\n"); }//i: line //print (k-1) blocks, each block has (k-1) lines for(i=0; i<block && line<=n; ++i){ ++starter; for(j=0; j<block; ++j, ++line){ printf("%d", starter); p = j; printf(" %d", k+p+1); for(t=(k+block+1); t<n; t+=block){ p = (p + i) % block; printf(" %d", t+p); }//t: go to starting point of each block printf("\n"); }//j: lines }//i: blocks of lines } return 0; }
No comments:
Post a Comment