69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
int main(void)
{
int a[10][10];
int x=0, y=0;
int n,s,c=1,i,j,n1;
printf("\nPlease input the width of the matrix:");
scanf("%d", &n);
n1 = n;
s = n*n;
while(c<=s){
for(i=0; i<n; ++i) //向右
a[y][x++] = c++;
--x;
--n;
++y;
for(i=0; i<n; ++i)
a[y++][x] = c++;
--y;
--x;
for(i=0; i<n; ++i) //向下
a[y][x--] = c++;
++x;
--n;
--y;
for(i=0; i<n; ++i) //向左
a[y--][x] = c++;
++y;
++x;
}
for(j=0; j<n1; ++j){ //向上
for(i=0; i<n1; ++i)
printf("%-3d", a[j][i]);
printf("\n");
}
return 0;
}