从代码上看,十楼的最好了,最有效率了!!!!!!!!!!!!!!!!
main(){ int i,u,x=0,y=0,n,z=1,a; scanf("%d",&n); a=n; for(i=0;i<n;i++){ z+=x; y=z; for(u=0;u<n;u++){ y+=(u<a)?u+x:n-u+a-1; printf("%5d",y); } x++; a--; printf("\n"); } }
int element(int n, int row, int col) { int k, s; k = row + col; s = k * (k + 1) / 2; if(k >= n) { k -= n - 1; s -= k * k; } return s + row + 1; } int main() { int i, j, n; scanf("%d", &n); for(i = 0; i < n; i++) { for(j = 0; j < n; j++) printf("%-5d", element(n, i, j)); printf("\n"); } return 0; }