海伦公式:三条边的边长为a、b、c,p=(a+b+c)/2,area=sqrt(p∗(p−a)∗(p−b)∗(p−c))三条边的边长为a、b、c,p=(a+b+c)/2,area=sqrt(p*(p-a)*(p-b)*(p-c))三条边的边长为a、b、c,p=(a+b+c)/2,area=sqrt(p∗(p−a)∗(p−b)∗(p−c))。
题目描述
已知三角形三个顶点在直角坐标系下的坐标分别为:
(2.3, 2.5)
(6.4, 3.1)
(5.1, 7.2)
求该三角形的面积。
输出
要求精确到小数后3位,如不足3位,需要补零。
代码如下:
#include <iostream>
#include <cmath>
using namespace std;double len(double x1, double y1, double x2, double y2) {return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}int main() {double x1 = 2.3, y1 = 2.5;double x2 = 6.4, y2 = 3.1;double x3 = 5.1, y3 = 7.2;double a = len(x1, y1, x2, y2);double b = len(x1, y1, x3, y3);double c = len(x2, y2, x3, y3);double p = (a + b + c) / 2;double area = sqrt(p * (p - a) * (p - b) * (p - c));printf("%.03lf\n", area);return 0;}