#include<cstring>
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int a[100][100] = {0};
int sum = 1, n, x, y;
cin >> n;
x = 0;
y = n - 1;
a[x][y] = sum;
while (sum < n * n)
{
while (x + 1 < n && a[x + 1][y] == 0)
{
sum++;
a[x + 1][y] = sum;
x++;
}
while (y - 1 >= 0 && a[x][y - 1] == 0)
{
sum++;
a[x][y - 1] = sum;
y--;
}
while (x - 1 >= 0 && a[x - 1][y] == 0)
{
sum++;
a[x - 1][y] = sum;
x--;
}
while (y + 1 < n && a[x][y + 1] == 0)
{
sum++;
a[x][y + 1] = sum;
y++;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%-6d", a[i][j]);
}
cout << endl;
}
return 0;
}
蛇形依次填入
最新推荐文章于 2024-11-16 13:27:03 发布