编写一个已知三角形三边求面积的代码
时间: 2023-09-23 16:07:12 浏览: 56
好的,这个问题是与数学相关的问题,我可以为您提供对应代码。以下是用海伦公式求解三角形面积的示例代码:
```python
import math
def triangle_area(a, b, c):
'''使用海伦公式计算三角形面积'''
s = (a + b + c) / 2
area = math.sqrt(s * (s-a) * (s-b) * (s-c))
return area
# 示例
a, b, c = 3, 4, 5 # 三角形的三边长度
area = triangle_area(a, b, c)
print('三角形面积为:', area)
```
希望这个代码能够满足您的需求!请问还有什么需要帮助的吗?
相关问题
用Python编写函数,已知三角形三边边长,求面积
可以使用海伦公式来计算三角形的面积,公式如下:
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
其中,a、b、c 分别为三角形的三条边长,s 为半周长,即 s = (a + b + c) / 2。
下面是使用 Python 编写的函数代码:
```python
from math import sqrt
def triangle_area(a, b, c):
s = (a + b + c) / 2
area = sqrt(s * (s - a) * (s - b) * (s - c))
return area
```
使用时,直接调用 `triangle_area` 函数,传入三角形的三条边长即可:
```python
>>> triangle_area(3, 4, 5)
6.0
```
这里以 (3, 4, 5) 为例,计算出来的面积应该为 6。
c语言编写已知三角形三边求内切圆半径和外切圆半径的代码
以下是C语言编写已知三角形三边求内切圆半径和外切圆半径的代码:
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, s, r, R;
printf("请输入三角形的三条边长:\n");
scanf("%lf%lf%lf", &a, &b, &c);
s = (a + b + c) / 2;
R = a * b * c / 4 / sqrt(s * (s - a) * (s - b) * (s - c));
r = sqrt((s - a) * (s - b) * (s - c) / s);
printf("内切圆半径为:%lf\n", r);
printf("外切圆半径为:%lf\n", R);
return 0;
}
注意:本AI只是一个角色扮演,不具备实际的智能,仅能根据预设的规则进行回答。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)