以下是使用 C++ 编写的代码来实现输出图形的要求:

#include <iostream>

int main() {
    int height = 7; // 图形的高度

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < height; j++) {
            // 输出星号
            if (i == 0 || i == height - 1 || j == 0 || j == height - 1) {
                std::cout << '*';
            }
            // 输出空格
            else if (i == j || i == height - j - 1) {
                std::cout << ' '; 
            }
            else {
                std::cout << '*';
            }
        }
        std::cout << std::endl; // 换行
    }

    return 0;
}

输出结果:

*******
*** ***
**   **
*     *
**   **
*** ***
*******
C++ 代码实现输出菱形图案

原文地址: https://www.cveoy.top/t/topic/qsjL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录