c++要求输出以下图案,利用循环语句 * *** ***** ******* ***** *** *
时间: 2023-11-23 12:07:11 浏览: 35
这是一个金字塔形状的图案,可以使用循环语句来实现。以下是C++代码实现:
```
#include <iostream>
using namespace std;
int main() {
int n = 4; // 控制金字塔高度
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j >= n - i + 1 && j <= n + i - 1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (j >= n - i + 1 && j <= n + i - 1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
输出结果为:
```
*
***
*****
*******
*****
***
*
```