c语言编程题(螺旋方阵)

来源:百度知道 编辑:UC知道 时间:2024/05/02 02:41:46
输出图形为;

1 1 2 3 4 5 6
2 20 21 22 23 24 7
3 19 32 33 34 25 8
4 18 31 36 35 26 9
5 17 30 29 28 27 10
6 16 15 14 13 12 11

//我选择了一个6*6的矩阵来打印,当然也可以通过修改
//参数打印n*n的矩阵。欢迎加我qq,共同讨论,进步!
//qq:237263394
#include<stdio.h>
main()
{
int array[6][6],t=0,m=0,r=0,c=0,s1=1,s2=5,s3=0,s4=5,i=1;
while(s1!=4)
{
while(m<=s4)
{
array[t][m]=i;
m++;
i++;
}
s4--;
m--;
i--;
while(t<=s2)
{
array[t][m]=i;
t++;
i++;
}
s2--;
t--;
i--;
while(m>=s3)
{
array[t][m]=i;
m--;
i++;
}
s3++;
m++;
while(t>s1)
{
t--;
array[t][m]=i;
i++;
}
t=s1;
m=s1;
s1++;
}
for(r=0;r<6;r++)
{ printf("%d ",r+1);
for(c=0;c<6;c++)
printf(" %d",array[r][c]);
printf("\n");
}
}