可以看一下这个程序:(已调试通过)
#include <stdio.h>
void main()
{
int data[10][10];
int n = 6;
int start, bound,k;
int i = 0;
int j = 0;
int loop = 0;
for(k = 1; k <= n*n; k++) {
data[i][j] = k;
bound = n - loop - 1;
start = loop;
if (i == start) {//left to right
if (j < bound) j ++;
else i ++;
}else if ((j == bound) && (i != bound)) {
if (i < bound) i ++;
else j ++;
}else if ((i == bound) && (j != start)) {
if (j > start) j --;
else i ++;
}else if ((j == start) && (i != start)) {
if (i > start+1) i --;
else j++, loop++;
}
}
for (i = 0; i < n ; i ++) {
for (j = 0; j < n; j++)
printf ("%4d",data[i][j]);
printf("\n");
}
}