用“C plus plus”语言编写边长为10个*的空心三角形,要求可以出现在指定位置。
时间: 2023-05-24 16:07:15 浏览: 53
以下是C++语言代码实现:
```c++
#include <iostream>
using namespace std;
int main()
{
int x, y; //指定位置
cout << "请输入三角形的横坐标位置x:" << endl;
cin >> x;
cout << "请输入三角形的纵坐标位置y:" << endl;
cin >> y;
// 输出边长为10的空心三角形
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= i; j++) {
if (i == 9 || j == 0 || j == i){ // 第1行和第i列为"*",其他为" "
cout << "* ";
} else if (i == (y - 1) && j == (x - 1)) { //指定位置处为"*"
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
```
首先需要用户输入指定位置的横纵坐标,然后使用两个嵌套的for循环来输出三角形。在循环中,条件判断语句用于控制空心和实心的位置,以及指定位置处的"*"。最后输出即可。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)