c++空心倒直角三角
时间: 2023-11-03 19:21:44 浏览: 211
要输出一个空心倒直角三角形,可以使用嵌套循环和条件语句来实现。以下是一个示例代码:
```c++
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
这段代码会根据用户输入的n的值输出相应层数的空心倒直角三角形。在每一行的循环中,我们使用条件语句判断当前位置是否需要输出"*"或者空格。如果是三角形的边界或者最后一行,则输出"*";否则输出空格。这样就能够实现空心的效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [C++ 输出倒三角星号](https://blog.csdn.net/weixin_45122104/article/details/123314862)[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_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [HDU - 2091 空心三角形 难度:C++入门 复杂度:简单](https://blog.csdn.net/weixin_33249666/article/details/113892541)[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_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文