生成如下矩阵:
1 2 3 4 5 6 7
24 25 26 27 28 29 8
23 40 41 42 43 30 9
22 39 48 49 44 31 10
21 38 47 46 45 32 11
20 37 36 35 34 33 12
19 18 17 16 15 14 13
int main(void)
{
int number;
scanf("%d", &number); //输入矩阵阶数
int a[100][100], i,j;
int flag = 1;
for (i = 1; i <= (number+1) / 2; i++) //计算总共有几圈
{
for (j = i; j <= number-i+1; j++) //上面的数
{
a[i][j] = flag;
flag++;
}
for (j = i; j <= number-i; j++) //右边的数
{
a[j+1][number-i+1] = flag;
flag++;
}
for (j = number-i; j >=i; j--) //下边的数
{
a[number-i+1][j] = flag;
flag++;
}
for (j = number-i; j > i; j--) //左边的数
{
a[j][i] = flag;
flag++;
}
}
int x, y; //输出
for (x = 1; x <= number; x++)
{
for (y = 1; y <= number; y++)
printf("%2d ",a[x][y]);
printf("\n");
}
return 0;
}