给定一个三角形的边,任务是求出该三角形的面积。
例如:
输入:a = 5, b = 7, c = 8
输出:三角形面积为 17.320508
输入:a = 3, b = 4, c = 5
输出:三角形面积为 6.000000
方法:可以使用以下公式简单地计算 三角形的面积。
![]()
其中 a、b 和 c 是三角形边长,
s = (a+b+c)/2

下面是上述方法的实现:
#include <stdio.h>
#include <stdlib.h>
float findArea(float a, float b, float c)
{
// Length of sides must be positive and sum of any two sides
// must be smaller than third side.
if (a < 0 || b < 0 || c <0 || (a+b <= c) ||
a+c <=b || b+c <=a)
{
printf("Not a valid triangle");
exit(0);
}
float s = (a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
int main()
{
float a = 3.0;
float b = 4.0;
float c = 5.0;
printf("Area is %f", findArea(a, b, c));
return 0;
}
输出
面积为 6
时间复杂度: O(log 2 n)
辅助空间: O(1),因为没有占用额外空间。
&spm=1001.2101.3001.5002&articleId=143477294&d=1&t=3&u=52247cd852be4b5f93d5478419960f29)
1万+

被折叠的 条评论
为什么被折叠?



