「经典图形题」集合 | C/C++

- 第 141 篇 -
Date: 2025 - 11- 02
Author: 郑龙浩(仟墨)

「经典图形题」集合 | C/C++

1. 正三角形(右对齐)

解释:输出一个向右对齐的三角形,底部最宽,顶部最窄

复制代码
输入:5
输出:
    *
   **
  ***
 ****
*****
cpp 复制代码
#include "bits/stdc++.h" 
using namespace std;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    int n; cin >> n;
    for (int i = 1; i <= n; i++) { // n行
        // 输出空格
        for (int j = 1; j <= n - i; j++) cout << ' ';
        // 输出*
        for (int j = 1; j <= i; j++) cout << '*';
        cout << '\n'; // 换行
    }   
    return 0;
}

2. 正三角形(居中)

解释:输出一个居中对齐的三角形

复制代码
输入:5
输出:
    *
   ***
  *****
 *******
*********

只需要将内层循环改成 j <= i * 2 - 1

cpp 复制代码
#include "bits/stdc++.h" 
using namespace std;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    int n; cin >> n;
    for (int i = 1; i <= n; i++) { // n行
        // 输出空格
        for (int j = 1; j <= n - i; j++) cout << ' ';
        // 输出*
        for (int j = 1; j <= i*2 - 1; j++) cout << '*';
        cout << '\n'; // 换行
    }   
    return 0;
}

3. 倒三角形(右对齐)

解释:与正三角形相反,顶部最宽,底部最窄

复制代码
输入:5
输出:
*****
 ****
  ***
   **
    *
cpp 复制代码
// 3_倒三角形(右对齐)
// Date:2025-11-02 Author:郑龙浩
#include "bits/stdc++.h" 
using namespace std;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    int n; cin >> n;
    for (int i = 1; i <= n; i++) { // n行
        // 输出空格.
        for (int j = 1; j < i; j++) cout << ' ';
        // 输出*
        for (int j = i; j <= n; j++) cout << '*';
        cout << '\n'; // 换行
    }   
    return 0;
}

4. 倒三角形(居中)

复制代码
输入:5
输出:
*********
 *******
  *****
   ***
    *
c 复制代码
// 关键:for (int j = i; j <= n * 2 - i; j++)
cpp 复制代码
// 4_倒三角形(居中)
// Date:2025-11-02 Author:郑龙浩
#include "bits/stdc++.h" 
using namespace std;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    int n; cin >> n;
    for (int i = 1; i <= n; i++) { // n行
        // 输出空格.
        for (int j = 1; j < i; j++) cout << ' ';
        // 输出*
        for (int j = i; j <= n * 2 - i; j++) cout << '*';
        cout << '\n'; // 换行
    }   
    return 0;
}

5. 菱形

上半部分是正三角,下半部分是倒三角

比如:输出9行的菱形,上5行是正三角,后四行是倒三角
注意:后四行倒三角,从第一行开始就有空格

复制代码
输出:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
cpp 复制代码
// 4_倒三角形(居中)
// Date:2025-11-02 Author:郑龙浩
#include "bits/stdc++.h" 
using namespace std;
const int n = 5, m = 4;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) cout << ' ';
        for (int j = 1; j <= i * 2 - 1; j++) cout << '*';
        cout << '\n';
    }
    for (int i = 1; i <= m; i++) { // n行
        // 输出空格.
        for (int j = 1; j <= i; j++) cout << ' ';
        // 输出*
        for (int j = i; j <= m * 2 - i; j++) cout << '*';
        cout << '\n'; // 换行
    }   
    return 0;
}

6. 空心菱形

解释:只输出菱形的边框,内部为空

复制代码
输入:5
输出:
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
cpp 复制代码
// 6_空心菱形
// Date:2025-11-02 Author:郑龙浩
#include "bits/stdc++.h" 
using namespace std;
const int n = 5, m = 4;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++) cout << ' ';
        for (int j = 1; j <= i * 2 - 1; j++) cout << (j == 1 || j == i * 2 - 1 ? '*' : ' ');
        cout << '\n';
    }
    for (int i = 1; i <= m; i++) { // n行
        // 输出空格.
        for (int j = 1; j <= i; j++) cout << ' ';
        // 输出* 
        for (int j = i; j <= m * 2 - i; j++) cout << (j == i || j == m * 2 - i ? '*' : ' ');
        cout << '\n'; // 换行
    }   
    return 0;
}

7. 对角线

解释:在n×n的矩阵中,从左上到右下的对角线

复制代码
输入:5
输出:
*    
 *   
  *  
   * 
    *                    
cpp 复制代码
// 7_正对角线
// Date:2025-11-02 Author:郑龙浩
#include "bits/stdc++.h" 
using namespace std;
const int n = 5, m = 4;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++)
            cout << (j == i ? "*" : " ");
        cout << '\n';
    }
    return 0;
}

7. 反对角线

解释:在n×n的矩阵中,从右上到左下的对角线

复制代码
输入:5
输出:
    *
   * 
  *  
 *   
*
cpp 复制代码
// 8_反对角线
// Date:2025-11-02 Author:郑龙浩
#include "bits/stdc++.h" 
using namespace std;
const int n = 5, m = 4;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    int n; cin >> n;
    for (int i = 5; i >= 1; i--) {
        for (int j = 1; j <= i; j++)
            cout << ' ';
        cout << "*\n";
    }
    return 0;
}

9. 正方形(实心/空心)

复制代码
实心(5×5):
*****
*****
*****
*****
*****

空心(5×5):
*****
*   *
*   *
*   *
*****
cpp 复制代码
// 9 正方形(实心/空心
// Date:2025-11-02 Author:郑龙浩
#include "bits/stdc++.h" 
using namespace std;
const int n = 5, m = 4;
int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cout << "输入宽度\n";
    int n; cin >> n;
    cout << "实心:\n";
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cout << "*";
        }
        cout << '\n';
    }
    cout << "空心:\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 << '\n';
    }
    return 0;
}
相关推荐
机器视觉的发动机11 分钟前
AI算力中心的能耗挑战与未来破局之路
开发语言·人工智能·自动化·视觉检测·机器视觉
HyperAI超神经19 分钟前
在线教程|DeepSeek-OCR 2公式/表格解析同步改善,以低视觉token成本实现近4%的性能跃迁
开发语言·人工智能·深度学习·神经网络·机器学习·ocr·创业创新
R_.L29 分钟前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
Zach_yuan38 分钟前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
我在人间贩卖青春41 分钟前
C++之this指针
c++·this
云姜.44 分钟前
java多态
java·开发语言·c++
CoderCodingNo1 小时前
【GESP】C++五级练习题 luogu-P1865 A % B Problem
开发语言·c++·算法
陳10301 小时前
C++:红黑树
开发语言·c++
VekiSon1 小时前
Linux内核驱动——杂项设备驱动与内核模块编译
linux·c语言·arm开发·嵌入式硬件
一切尽在,你来1 小时前
C++ 零基础教程 - 第 6 讲 常用运算符教程
开发语言·c++