top1编程
← 返回题目
题解

【入门】空心正方形

1 条题解

  • 0
    @ 2026-7-29 0:15:21
    #include <iostream>
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
    
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= n; ++j) {
                // 第一行、最后一行、第一列、最后一列打印 *
                if (i == 1 || i == n || j == 1 || j == n) {
                    cout << "*";
                } else {
                    cout << " ";
                }
            }
            cout << endl;
        }
    
        return 0;
    }
    
    • 1