社区讨论

稻花香里说丰年,听取WA声一片!

P2615[NOIP 2015 提高组] 神奇的幻方参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@lo2bl1xe
此快照首次捕获于
2023/10/23 11:10
2 年前
此快照最后确认于
2023/11/03 11:19
2 年前
查看原帖
CPP
#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;
    }
}

回复

1 条回复,欢迎继续交流。

正在加载回复...