B3924 [GSEP202312 二级] 小杨的H字矩阵 题解

· · 题解

题目分析:

  1. 如果现在要输出的地方是这一行的最后一个或者第一个,则输出 |。

  2. 如果现在要输出的地方是在 \frac{N+1}{2} 行,且不是这一行的最后一个或者第一个,那么则输出 -。

  3. 如前两条都未成立,即输出 a。

AC 代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int mid=(n+1)/2;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(j==1 || j==n)
            {
                cout<<"|";
            }
            else if(i==mid)
            {
                cout<<"-";
            }
            else
            {
                cout<<"a";
            }
        }
        if(i!=n) cout<<endl;
    }
    return 0;
}