一本通1034:计算三角形面积

题目传送门

【题目描述】

平面上有一个三角形,它的三个顶点坐标分别为(x1,y1),(x2,y2),(x3,y3),那么请问这个三角形的面积是多少,精确到小数点后两位。

【输入】

输入仅一行,包括6个单精度浮点数,分别对应x1,y1,x2,y2,x3,y3。

【输出】

输出也是一行,输出三角形的面积,精确到小数点后两位。

【输入样例】

0 0 4 0 0 3

【输出样例】

6.00

分析:此题需要用两点之间距离公式求出来三边分别的长度,然后用海伦公式求出其面积


Code:

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cmath>
using namespace std;
int main(){
	float x1,y1,x2,y2,x3,y3;
	cin>>x1>>y1>>x2>>y2>>x3>>y3;
	double a=sqrt(pow((x1-x2),2)+pow((y1-y2),2));
	double b=sqrt(pow((x2-x3),2)+pow((y2-y3),2));
	double c=sqrt(pow((x1-x3),2)+pow((y1-y3),2));
	double p=(a+b+c)/2;
	cout<<fixed<<setprecision(2)<<sqrt(p*(p-a)*(p-b)*(p-c));
	return 0;
}
posted @ 2020-06-16 20:21  _pwl  阅读(1293)  评论(0编辑  收藏  举报
1 2 3
4