a、假设三角形的边长分别为3cm、5cm和7cm,求出三角形的长度和面积。用c++做
时间: 2024-02-20 17:01:30 浏览: 42
根据海伦公式,可以计算出半周长s=(3+5+7)/2=7.5cm,然后三角形的面积S=sqrt(s(s-a)(s-b)(s-c)),其中a、b、c分别为三角形的边长,代入计算可得S=6.4952cm²。以下是用C++实现的代码:
```
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a = 3, b = 5, c = 7;
double s = (a + b + c) / 2;
double area = sqrt(s * (s - a) * (s - b) * (s - c));
cout << "Triangle perimeter: " << a + b + c << "cm" << endl;
cout << "Triangle area: " << area << "cm²" << endl;
return 0;
}
```
输出结果为:
```
Triangle perimeter: 15cm
Triangle area: 6.4952cm²
```
相关问题
python已知三角形的三条边长分别为8cm、10cm和12cm,求此三角形的面积。
根据海伦公式,已知三角形三边长 a、b、c,令 p = (a+b+c)/2,则三角形面积 S = √(p(p-a)(p-b)(p-c))。
将 8cm、10cm、12cm 代入公式,得:
p = (8+10+12)/2 = 15
S = √(15(15-8)(15-10)(15-12)) ≈ 48cm²
因此,此三角形的面积约为 48平方厘米。
用c++代码写输入直角三角形两直角边长求斜边长度周长和面积
可以使用勾股定理来计算斜边长度,代码如下:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, perimeter, area;
printf("请输入直角三角形的两条直角边长:\n");
scanf("%lf %lf", &a, &b);
c = sqrt(a * a + b * b);
perimeter = a + b + c;
area = a * b / 2;
printf("斜边长度为:%.2lf\n", c);
printf("周长为:%.2lf\n", perimeter);
printf("面积为:%.2lf\n", area);
return 0;
}
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)