二维数组输出2 c++

二维数组输出2
https://acm.songpy.top/p/P1264
题目描述
输入一个整数N,输出一个N行 N列的二维矩阵,矩阵中的元素用\1 ~ N*N顺序螺旋填充。
输入格式
一个整数N(N<=10)
输出格式
输出N行 N列 的矩阵,元素之间用一个空格隔开,行末不要有多余的空格。
样例
输入数据
3
输出数据
1 2 3
8 9 4
7 6 5

#include<iostream>
using namespace std;
int main()
{
    int n,cnt=0;
    int x=1,y=1;
    int a[15][15];
    cin>>n;
    for(int i=n-1;i>0;i-=2)
    {
        for(int j=1;j<=i;j++)
        {
            a[x][y]=++cnt;
            y++;
          }
        for(int j=1;j<=i;j++)
        {
            a[x][y]=++cnt;
            x++;
        }
        for(int j=1;j<=i;j++)
        {
            a[x][y]=++cnt;
            y--;
        }
        for(int j=1;j<=i;j++)
        {
            a[x][y]=++cnt;
            x++;
        }
        x++;
        y++;
        if(n%2==1)
            a[x][y]=++cnt;  
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            cout<<a[i][j]<<' ';
        cout<<endl;
    }
}

输入:5 输出:1 2 3 4 5
0 0 0 0 6
0 0 0 0 7
0 0 0 0 8
13 12 11 10 9

https://blog.csdn.net/hechilai/article/details/110248062
希望他会帮助到你哦!