c++如何写出等边三角形
时间: 2024-05-30 19:04:37 浏览: 20
以下是C++中输出等边三角形的示例代码:
```c++
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "请输入等边三角形的行数:";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
}
cout << endl;
}
return 0;
}
```
相关问题
C++,用代码写出一个三角形
下面是用C语言代码实现的一个简单三角形:
```
#include <stdio.h>
int main() {
printf(" /\\\n");
printf(" / \\\n");
printf(" /____\\\n");
return 0;
}
```
输出结果:
```
/\
/ \
/____\
```
用c++语法写出二维数组杨辉三角形
好的,以下是用 C 语法写的杨辉三角形二维数组:
```
#include<stdio.h>
int main()
{
int rows, coef = 1;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(int i=0;i<rows;i++)
{
for(int j=0;j<=i;j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j; // Using Pascal's Formula
printf("%d ",coef);
}
printf("\n");
}
return 0;
}
```
以上代码可以输出指定行数的杨辉三角形二维数组。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![asm](https://img-home.csdnimg.cn/images/20210720083646.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://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)