跳到正文

题解

题解:B4162 [BCSP-X 2024 12 月初中组] 稳定原子

felixzou约 2 分钟
原文
文章信息与版本

文章信息

发布
2025年4月4日
收录
2025年12月12日
更新
2026年9月16日
篇幅
约 2 分钟
获赞
1
题目
B4162

版本

  1. v1当前

这道题分类讨论一下即可,考虑的是 nn 是奇是偶。

如果 nn 是奇数,直接输出 No。

如果 nn 是偶数,输出 Yes,最外面的一圈填上 11,往里一层的一圈填上 00,再往里一层的一圈填上 11,再往里一层的一圈填上 00,以此类推,直到填满中间的 2×22\times2 的格子 (也就是从外往里 1,01,0 交替)。

以下是这道题的 AC 代码:

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int N=1010;
int a[N][N];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        if(n%2==1){
            cout<<"No\n";
        }else{
            cout<<"Yes\n";
            int l=1,r=n,flag=1;
            while(l<r){
                for(int i=l;i<=r;i++){
                    for(int j=l;j<=r;j++){
                        a[i][j]=flag;
                    }
                }
                flag=1-flag;
                l++,r--;
            }
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                    cout<<a[i][j]<<" ";
                }
                cout<<"\n";
            }
        }
    }
    return 0;
}
原文