用“c plus plus”编写一个可在指定位置输出的边长为10*的空心等边三角形
时间: 2023-05-25 12:04:38 浏览: 64
以下是使用C++编写可在指定位置输出的边长为10*的空心等边三角形的程序:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
int row, col, start_col;
string space = " "; // 10 spaces
cout << "Please enter the row number for the triangle: ";
cin >> row;
cout << "Please enter the column number to start the triangle: ";
cin >> start_col;
// loop for printing each row of the triangle
for (int i = 0; i < row; i++) {
col = start_col;
// print spaces before the triangle starts
cout << space.substr(0, col);
// print the top row of the triangle
if (i == 0) {
cout << "*" << endl;
continue;
}
// print each row of the triangle
for (int j = 0; j < i + 1; j++) {
if (j == 0 || j == i) {
cout << "*"; // print the first and last character
} else if (i == row - 1) {
cout << "*"; // print the bottom row of the triangle
} else {
cout << " "; // print spaces to create the hollow part of the triangle
}
}
cout << endl;
}
return 0;
}
```
在这个程序中,用户需要输入三个参数:三角形的行数,三角形的起始列数和三角形的起始行数。`space`字符串包含10个空格,用于在三角形前面打印空格。然后,程序使用两个嵌套循环来打印三角形。外部循环用于打印每一行,内部循环用于打印每一行中的字符。根据字符的位置,程序将打印星号或空格。当程序到达三角形的底部时,它将打印完整的行。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)