标签:io for amp c return print include ca
/*蛇形填数
输入一个n,将1-n*n全部输入,例如,当n=5时,输出:
13 14 15 16 1
12 23 24 17 2
11 22 25 18 3
10 21 20 19 4
9 8 7 6 5
*/
#include<stdio.h>
int a[100][100]={0};
int main()
{
int x,y,i,j,p,n;
scanf("%d",&n);
p=a[x=0][y=n-1]=1;
while(p<n*n)//注意是++x,++y;
{
while(x+1<n&&!a[x+1][y]) a[++x][y]=++p;
while(y-1>=0&&!a[x][y-1]) a[x][--y]=++p;
while(x-1>=0&&!a[x-1][y]) a[--x][y]=++p;
while(y+1<n&&!a[x][y+1]) a[x][++y]=++p;
}
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
printf("%3d",a[x][y]);
}
printf("\n");//注意换行
}
while(1);
return 0;
}
标签:io for amp c return print include ca
原文地址:http://blog.csdn.net/hdd871532887/article/details/38346197