稻花香里说丰年,听取WA声一片!
查看原帖
稻花香里说丰年,听取WA声一片!
601214
mlgjzst楼主2023/7/6 15:35
#include<iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int ans[45][45] = {0};
    int lastx = 1;
    int lasty = n/2;
    for(int i = 1;i <= n*n;i++)
    {
        if(i==1)
        {
            ans[1][n/2] = 1;
            int lastx = 1;
            int lasty = n/2;
        }
        else if(lastx == 1 && lasty != n - 1)
        {
            lastx = n - 1;
            lasty++;
            ans[lastx][lasty] = i;
        }
        else if(lasty == n - 1 && lastx != 1)
        {
            lastx++;
            lasty = 1;
            ans[lastx][lasty] = i;
        }
        else if((lastx == 1 && lasty == n - 1) || !ans[lastx + 1][lasty + 1])
        {
            lastx++;
            ans[lastx][lasty] = i;
        }
        else
        {
            lastx--;
            lasty++;
            ans[lastx][lasty] = i;
        }
    }
    for(int i = 0;i < n;i++)
    {
        for(int j = 0;j < n;j++)
        {
            cout << ans[i][j] << " ";
        }
        cout << endl;
    }
}
2023/7/6 15:35
加载中...