螺旋矩阵
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <random>
using namespace std;
#define N 30
void f(int a[][N], int n)
{
int m, k = 0;
if (n % 2 == 0)
m = n / 2;
else
m = n / 2 + 1;
for (int i = 0; i < m; i++)
{
for (int j = i; j < n - i; j++)
a[i][j] = ++k;
for (int j = i + 1; j < n - i; j++)
a[j][n - i - 1] = ++k;
for (int j = n - i - 2; j >= i; j--)
a[n - i - 1][j] = ++k;
for (int j = n - i - 2; j >= i + 1; j--)
a[j][i] = ++k;
}
}
int main()
{
int a[N][N], n;
cin >> n;
f(a, n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
输出