当前位置: 首页 > news >正文

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

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

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

1. 正三角形(右对齐)

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

输入:5
输出:**********
*****
#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

#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
输出:
***************
// 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
输出:
*************************
// 关键:for (int j = i; j <= n * 2 - i; j++)
// 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行是正三角,后四行是倒三角
注意:后四行倒三角,从第一行开始就有空格

输出:****************
*************************
// 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
输出:** **   **     *
*       **     **   ** **
// 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
输出:
*    *   *  * *                    
// 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
输出:** *  *   
*
// 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):
*****
*   *
*   *
*   *
*****
// 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;
}
http://www.dtcms.com/a/561256.html

相关文章:

  • IT4IT是由The Open Group提出的面向数字化转型的IT管理参考架构框架
  • 学校网站怎么做的好南翔做网站公司
  • 解决 CentOS 8 报错:Failed to download metadata for repo ‘BaseOS‘
  • VS Code集成googletest-C/C++单元测试Windows
  • Vue 图片性能优化双剑客:懒加载与自动压缩实战指南
  • 网站之家查询qq空间网站是多少
  • Elasticsearch 与 Faiss 联合驱动自动驾驶场景检索:高效语义匹配 PB 级视频数据
  • 短租网站开发学术ppt模板免费
  • 设计模式——单例模式(singleton)
  • 【计算机软件资格考试】软考综合知识题高频考题及答案解析1
  • 计算机网络自顶向下方法25——运输层 TCP流量控制 连接管理 “四次挥手”的优化
  • LeetCode【高频SQL基础50题】
  • 清远做网站的有哪些wordpress判断浏览器
  • 自己做的网站怎样才有网址浏览找人做网站域名怎么过户
  • JavaScript中的闭包:原理与实战
  • 怎么看一个网站是否被k怎么找项目
  • 交易网站开发做的比较好的p2p网站
  • JavaScript异步编程:从回调地狱到优雅解决方案
  • 【MATLAB】matlab闪退问题(随时更新)
  • 有专门做最佳推荐的网站东莞网站制作十年乐云seo
  • React中的stopPropagation和preventDefault
  • React Hooks:提升前端开发效率的关键
  • Apache Tomcat 介绍
  • 国网公司网站建设宠物网站的目的
  • 怎么找做网站的外包公司二级域名是什么
  • CentOS 7/8/9 一键安装 Python 3.10+ 并配置默认版本
  • Harmony鸿蒙开发0基础入门到精通Day08--JavaScript篇
  • OpenCV(十八):绘制文本
  • Arbess实践指南(3) - 使用Arbess+sourcefare+PostIn实现Java项目自动化部署 + 代码扫描 + 接口自动化测试
  • 一,PCB介绍