https://ok.hn.cn/p/GESP2312C2B

问题描述

image.png

输入描述

一行一个整数 N( 5≤N≤49,保证 N 为奇数)。

输出描述

输出对应的“H 字矩阵”。 请严格按格式要求输出,不要擅自添加任何空格、标点、空行等任何符号。你应该恰好输出 N行,每行除了换行符外恰好包含 N个字符,这些符要么是 - ,要么是 | ,要么是 a 。你的输出必须和标准答案完全一致才能得分, 请在提交前仔细检查。

特别提醒

在常规程序中,输入、输出时提供提示是好习惯。但在本场考试中,由于系统限定,请不要在输入、输出中附带任 何提示信息。

题解

可以先按行、列输出 全部为’a’ 或 ‘-’ 是 n 行 n 列图形,再通过整理 ‘|’ 所在的行列关系,做条件判断进行替换输出

#include <bits/stdc++.h>  
using namespace std;  
int main() {  
    int n;  
    cin >> n;  
    for(int i = 1; i <= n; i++) {  
        for(int j = 1; j <= n; j++) {  
            if (j == 1 || j == n) {//第一列或者最后一列,输出|  
                cout << '|';  
            } else if (i == n / 2 + 1) {//中间行,输出-  
                cout << '-';  
            } else {//其他,输出a  
                cout << 'a';  
            }  
        }  
        cout << endl;  
    }  
    return 0;  
}  

视频题解

n = int(input())  
  
for i in range(1,n+1):  
    if i == (n+1) // 2:  
        print("|","-"*(n-2),"|",sep="")  
    else:  
        print("|","a"*(n-2),"|",sep="")