您的位置:首页 > 其它

题目33 蛇形填数

2016-07-28 12:02 211 查看


    已AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
int a

, top=1, x, y;
x = -1;
y = n-1;
memset(a, 0, sizeof(a));
while(top <= n*n)
{
while(x<n-1 && !a[x+1][y]) a[++x][y] = top++;
while(y>0 && !a[x][y-1]) a[x][--y] = top++;
while(x>0 && !a[x-1][y]) a[--x][y] = top++;
while(y<n && !a[x][y+1]) a[x][++y] = top++;
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
printf("%d ", a[i][j]);
printf("\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  南阳理工OJ