「经典图形题」集合 | 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;
}
相关推荐
周杰伦_Jay1 天前
【Eino框架】Go语言驱动的LLM应用开发新范式
开发语言·后端·golang
上78将1 天前
Java中既有编译执行又有解释执行,这个怎么理解?
java·开发语言
一个无名的炼丹师1 天前
【硬核实战】Python处理多源异构文档:从读取到智能信息提取的统一框架深度剖析
开发语言·python
Mr_Xuhhh1 天前
JAVA期末重点
java·开发语言·python
a程序小傲1 天前
小红书Java面试被问:java创建对象有哪些方式?
java·开发语言·面试
行走的陀螺仪1 天前
JavaScript 装饰器完全指南(原理/分类/场景/实战/兼容)
开发语言·javascript·ecmascript·装饰器
AA陈超1 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-18.生成火球术
c++·游戏·ue5·游戏引擎·虚幻
CreasyChan1 天前
C# 委托/事件/UnityEvent 详解
开发语言·c#
whm27771 天前
Visual Basic 建立数据库
开发语言·数据库·visual studio
wxin_VXbishe1 天前
springboot居家养老管理系统-计算机毕业设计源码55953
java·c++·spring boot·python·spring·django·php