用string方法打印以下图案: * * * * *
时间: 2024-05-25 12:07:56 浏览: 23
可以使用以下代码来打印该图案:
```
#include <iostream>
#include <string>
int main() {
std::string pattern = "* * * * *";
std::cout << pattern << std::endl;
return 0;
}
```
这个程序将字符串 `"* * * * *"` 赋给了一个名为 `pattern` 的字符串变量,并通过 `cout` 输出了该字符串。输出结果为:
```
* * * * *
```
相关问题
用c++用string打印以下图案: * * * * * * * * * * * * * * * * * * * * * * * * *
以下是用C++中的string打印该图案的代码:
```
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 5; // 控制行数
int m = 0; // 控制空格数
for (int i = 0; i < n; i++) {
string line = ""; // 每行输出的字符串
for (int j = 0; j < m; j++) {
line += " ";
}
for (int k = 0; k < n; k++) {
line += "* ";
}
cout << line << endl;
m++; // 空格数加1
}
return 0;
}
```
用c++的string写代码打印以下图案: * * * * * * * * * * * * * * * * * * * * * * * * *
以下是使用C++的string输出图案的代码,题目要求中的空格是由字符串的构造函数和append方法产生的:
```cpp
#include <iostream>
#include <string>
int main()
{
std::string space = " ";
std::string star = "* ";
for (int i = 0; i < 5; i++)
{
std::string output = space;
output.append(i * 2, space);
output.append(5 - i, star);
std::cout << output << std::endl;
}
return 0;
}
```
输出结果:
```
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)