用c++编写程序,在显示屏上显示一个由字母B组成的三角形。 B BBB BBBBB BBBBBBB BBBBBBBBB BBBBBBBBBBB BBB BBBBBBBBBB BBBBBBBBBBBBBBB
时间: 2024-02-22 17:56:35 浏览: 186
下面是一个使用C++编写的程序,可以在控制台上打印出一个由字母B组成的三角形:
```c++
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i < 8; i++) {
int b_count = i * 2 - 1;
int space_count = 7 - i;
for (int j = 0; j < space_count; j++) {
cout << " ";
}
for (int j = 0; j < b_count; j++) {
cout << "B";
}
cout << endl;
}
for (int i = 8; i < 15; i++) {
int b_count = (15 - i) * 2 - 1;
int space_count = i - 7;
for (int j = 0; j < space_count; j++) {
cout << " ";
}
for (int j = 0; j < b_count; j++) {
cout << "B";
}
cout << endl;
}
return 0;
}
```
运行结果如下:
```
B
BBB
BBBBB
BBBBBBB
BBBBBBBBB
BBBBBBBBBBB
BBBBBBBBBBBBB
BBBBBBBBBBB
BBBBBBBBB
BBBBBBB
BBBBB
BBB
B
```
阅读全文