代码找不出问题但是出不了结果
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<string.h>
#define max 100
int a[max][max];
int main()
{
int x, y;
int n;
int start;
scanf("%d",&n);
memset(a, 0, sizeof(a));
x = 0;
y = n - 1;
start = a[x][y] = 1;
while (start = n * n)
{
while (x + 1 < n && !a[x + 1][y])a[++x][y] = ++start;
while (y - 1 >=0 && !a[x][y-1])a[x][--y] = ++start;
while (x-1>= 0 && !a[x - 1][y])a[--x][y] = ++start;
while (y + 1 < n && !a[x + 1][y])a[++x][y] = ++start;
}
for (x = 0; x < n; x++)
{
for (y = 0; y < n; y++)
printf("%3d", a[x][y]);
printf("\n");
}
return 0;
}
17行循环结束条件不对
修改处见注释,供参考:
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<string.h>
#define max 100
int a[max][max];
int main()
{
int x, y;
int n;
int start;
scanf("%d",&n);
memset(a, 0, sizeof(a));
x = 0;
y = n - 1;
start = a[x][y] = 1;
while (start < n * n)//while (start = n * n) 修改
{
while (x + 1 < n && !a[x + 1][y]) a[++x][y] = ++start;
while (y - 1 >=0 && !a[x][y-1]) a[x][--y] = ++start;
while (x-1>= 0 && !a[x - 1][y]) a[--x][y] = ++start;
while (y + 1 < n && !a[x][y+1]) a[x][++y] = ++start;//修改
//while (y + 1 < n && !a[x + 1][y]) a[++x][y] = ++start;
}
for (x = 0; x < n; x++)
{
for (y = 0; y < n; y++)
printf("%3d", a[x][y]);
printf("\n");
}
return 0;
}