总觉得我以前在这里就写过类似的(好像还简单分析过),可找不到了。再写一个。
程序代码:

#include<stdio.h> int cal(int n, int row, int col) { int d, t; if(row <= col) { d = row < n - 1 - col ? row : n - 1 - col; t = row + col - d * 2; } else { d = col < n - 1 - row ? col : n - 1 - row; t = (n - 1 - d * 2) * 4 - (row + col - d * 2); } return (n - d) * d * 4 + t; } int main() { int n, i, j; scanf("%d", &n); for(i = 0; i < n; i++, puts("")) for(j = 0; j < n; j++) printf("%4d", cal(n, i, j)); return 0; }

重剑无锋,大巧不工