代码求助
  • 板块题目总版
  • 楼主L_Legend
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/7/8 11:43
  • 上次更新2023/11/4 18:25:41
查看原帖
代码求助
533879
L_Legend楼主2021/7/8 11:43
#include<bits/stdc++.h>
using namespace std;
const int N = 40;
int a[N][N], num = 1, n, x, y;
int main()
{
    scanf("%d", &n);
    n = n * 2 - 1;
    while (num <= n * n) {
        if (num == 1)a[x = 1][y = (n+1)/2] = num++;
        else if(x == 1 && y != n)a[x = n][y = y+1] = num++;
        else if(x != 1 && y == n)a[x = x-1][y = 1] = num++;
        else if(x == 1 && y == n)a[x = x+1][y = y] = num++;
        else if(!a[x-1][y+1])a[x = x-1][y = y+1] = num++;
        else a[x = x+1][y = y] = num++;
    }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            printf("%d%c", a[i][j], j == n ? '\n' : ' ');
    return 0;
}
2021/7/8 11:43
加载中...