C#海伦公式三边计算三角形面积
海伦公式
S = p ( p − a ) ( p − b ) ( p − c ) p = a + b + c 2 S = \sqrt{p(p-a)(p-b)(p-c)} \\ p = \frac{a+b+c}{2} S=p(p−a)(p−b)(p−c)p=2a+b+c
参考代码
using System;
namespace T
{
class TriangleArea
{
// 通过三条边计算三角形面积
static void Main(string[] args)
{
double a, b, c, p, s;
Console.WriteLine("请输入a,b,c:");
a = Double.Parse(Console.ReadLine());
b = Double.Parse(Console.ReadLine());
c = Double.Parse(Console.ReadLine());
p = (a + b + c) / 2;
s = Math.Sqrt((p - a) * (p - b) * (p - c) * p);
Console.WriteLine($"面积为:{s}");
}
}
}
请输入a,b,c:
3
4
5
面积为:6