小洛的田字矩阵

· · 题解

欢迎报名洛谷网校,期待和大家一起进步!

考点:循环嵌套

解析:使用二重循环枚举在第 i 行第 j 列填写什么字符:

参考代码:

#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 == 1 || i == n)
                cout << '-';
            else if (i == (n + 1) / 2 && j != (n + 1) / 2)
                cout << '-';
            else if (j == (n + 1) / 2 && i != (n + 1) / 2)
                cout << '|';
            else
                cout << 'x';
        }
        cout << endl;
    }
    return 0;
}