打印星号三角形 c++
时间: 2023-09-05 12:13:09 浏览: 295
以下是一个打印星号三角形的C++程序:
```cpp
#include <iostream>
using namespace std;
int main() {
int n;
char c;
cout << "How many lines? ";
cin >> n;
cout << "What character? ";
cin >> c;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= n - 1 - i; j++) {
cout << " ";
}
for (int j = 1; j <= i * 2 - 1; j++) {
cout << c;
}
cout << endl;
}
return 0;
}
```
这段代码会首先询问用户要打印的行数和字符,然后使用嵌套循环打印出相应的星号三角形。外层循环控制行数,内层循环控制每行中字符的数量。在每行中,首先打印一定数量的空格,然后打印一定数量的字符。最后,输出一个换行符,进入下一行的打印。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [东方博易OJ——1006 - 【入门】打印星号三角形](https://blog.csdn.net/zjx120307/article/details/126435191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [PTA C++ 输出星号组成的三角形](https://blog.csdn.net/Moriafly/article/details/117918174)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [C++实验题:打印三角形图案](https://download.csdn.net/download/wym627/3234667)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文