您的位置:首页 > 其它

关于蛇形填数问题求解

2015-12-11 17:35 274 查看
在n*n方阵里填入1, 2, 3, 4, 5...........n*n,填成蛇形,如:

n = 4时

10 11 12 1

9 16 13 2

8 15 14 3

7 6 5 4

代码如下

#include <cstdio>
#include <cstring>
#define MAXN 10
int a[MAXN][MAXN];
//using namespace std;
int main()
{
int n, x, y, m, tot;
//cin >> n;
scanf("%d",&n);
memset(a, 0, sizeof(a)) ;//数组清零
tot = a[x=0][y=n-1]=1;
while(tot<n*m)
{
while(x+1<n&&!a[x+1][y])
a[++x][y]=++tot;
while(y-1>=0&&!a[x][y-1])
a[x][--y]=++tot;
while(x-1>=0&&!a[x-1][y])
a[x][--y]=++tot;
while(y+1<n&&!a[x][y+1])
a[x][++y]=++tot;
}
for(int i=0;i<n;++i)
{
for(int j=0;j<n;++j)
printf("%3d", a[i][j]);
printf("\n");
}
return 0;
}


检查结果;

Compilation results...

--------

- Errors: 0

- Warnings: 0

- Output Filename: C:\Documents and Settings\Administrator\My Documents\未命名1.exe

- Output Size: 500.2529296875 KiB

- Compilation Time: 0.34s

运行时输入n后

结果却是一片空白

是不是哪里有错

求指导

谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: