文章
79
粉丝
221
获赞
46
访问
198.4k
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n;
cin >> n;
int** a = new int* [n];
for (int i = 0; i < n; i++) {
a[i] = new int[n];
for (int j = 0; j < n; j++)
a[i][j] = 0;
}
int k = 1, i = 0, j = 0;
while (k <= n * n) {
while (i < n && a[i][j] == 0 && k <= n * n)
a[i++][j] = k++;
if (k > n * n * n)
break;
i--, j++;
while (j < n && a[i][j] == 0 && k <= n * n)
a[i][j++] = k++;
if (k > n * n)
break;
j--, i--;
while (i >= 0 && a[i][j] == 0 && k <= n * n)
a[i--][j] = k++;
if (k > n * n)
break;
i++, j--;
while (j >= 0 && a[i][j] == 0 && k <= n * n)
a[i][j--] = k++;
if (k > n * n)
break;
j++, i++;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cout << setw(4) << left << a[i][j];
cout <...
登录后发布评论
暂无评论,来抢沙发