打印以图案: * * * * * * * * * * * * * * * * * * * * * * * * * (1)用字符数组方法;(2)用string方法。c++
时间: 2024-05-12 09:12:20 浏览: 101
以下是两种C++打印该图案的方法:
引用:
```cpp
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i * 2; j++) {
cout << " ";
}
cout << "* * * * *" << endl;
}
return 0;
}
```
引用[2]:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 4;
int sum;
char ch = ' ';
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 3 * (n - i); j++) {
cout << " ";
}
cout << '"';
for (sum = 2 * i - 1; sum >= 2; sum--) {
cout << ch << ch << ch;
}
cout << endl;
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= 3 * (n - i); j++) {
cout << " ";
}
cout << '"';
for (sum = 2 * i - 1; sum >= 2; sum--) {
cout << ch << ch << ch;
}
cout << endl;
}
return 0;
}
```
阅读全文