蛇形填数问题:输入一个整数n,按照蛇形填写n*n的矩阵
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main2()
{
int n, x, y, total;
cin >> n;
int** a = new int* [n];//定义动态二维数组
for (int i = 0; i < n; i++)
{
a[i] = new int[n];//按行分配空间
memset(a[i], 0, n * sizeof(int));//将元素都赋值0
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << setw(5) << a[i][j] ;//格式控制
}
cout << endl;
}
cout << endl;
x = y = 0;
total = a[0][0] = 1;
while (total < n * n)
{
while (y + 1 < n && !a[x][y + 1])//向右
a[x][++y] = ++total;
while (x + 1 < n && !a[x+1][y])//向下
a[++x][y] = ++total;
while (y - 1 >=0 && !a[x][y - 1])//向左
a[x][--y] = ++total;
while (x-1>= 0 && !a[x-1][y])//向上
a[--x][y] = ++total;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << setw(5) << a[i][j];
}
cout << endl;
}
for (int i = 0; i < n; i++)
delete[] a[i];//按行释放空间
delete[] a;
return 0;
}