菜鸡提问
查看原帖
菜鸡提问
767048
Rookie_h楼主2022/8/22 17:31
#include <bits/stdc++.h>
using namespace std;
int p[9][9];
int main()
{
    int n;
    cin >> n;
    p[1][1] = 1;
    for (int i = 1, j = 1, s = 1; s < n * n;)
    {
        while (++j <= n && !p[i][j])
            p[i][j] = ++s;
        --j;
        while (++i <= n && !p[i][j])
            p[i][j] = ++s;
        --i;
        while (--j > 0 && !p[i][j])
            p[i][j] = ++s;
        ++j;
        while (--i > 0 && !p[i][j])
            p[i][j] = ++s;
        ++i;
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (p[i][j] < 10)
                cout << ' ' << ' ' << p[i][j];
            else if (p[i][j] >= 10)
                cout << ' ' << p[i][j];
            else
                cout << p[i][j];
        }
        cout << endl;
    }
}

为什么把p[9][9] 定义为全局变量的时候 代码就有输出 定义在main里面的时候就没有输出呢

2022/8/22 17:31
加载中...