求大佬帮忙~
查看原帖
求大佬帮忙~
405146
Ruan_ji楼主2022/7/3 10:04
#include <iostream>
#include <cstdio>
#define MAXN 40
using namespace std;
int n, answer[MAXN][MAXN], x, y;
int main(){
    cin >> n;
    answer[1][(n + 1) / 2] = 1;
    x = 1;
    y = (n + 1) / 2;
    for (int i = 2;i <= n * n; ++i){
        if (x == 1 && y != n){
            answer[n][y + 1] = i;
            x = n;
            y += 1;
        }
        else if (x != 1 && y == n){
            answer[x - 1][1] = i;
            x -= 1;
            y = 1;
        }
        else if (x == 1 && y == n){
            answer[x][y + 1] = i;
            y += 1;
        }
        else if (x != 1 && y != n){
            if (answer[x - 1][y + 1] == 0){
                answer[x - 1][y + 1] = i;
                x -= 1;
                y += 1;
            }
            else {
                answer[x + 1][y] = i;
                x += 1;
            }
        }
    }
    for (int i = 1;i <= n; ++i){
        for (int j = 1;j <= n; ++j){
            cout << answer[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
2022/7/3 10:04
加载中...