三角形面积
时间限制:3000 ms | 内存限制:65535 KB
难度:2
描述
给你三个点,表示一个三角形的三个顶点,现你的任务是求出该三角形的面积
输入
每行是一组测试数据,有6个整数x1,y1,x2,y2,x3,y3分别表示三个点的横纵坐标。(坐标值都在0到10000之间)
输入0 0 0 0 0 0表示输入结束
测试数据不超过10000组
输出
输出这三个点所代表的三角形的面积,结果精确到小数点后1位(即使是整数也要输出一位小数位)
样例输入
0 0 1 1 1 3
0 1 1 0 0 0
0 0 0 0 0 0样例输出
1.0
0.5
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c,d,e,r,s;
while(1)
{
scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&r);
if(a==0&&b==0&&c==0&&d==0&&e==0&&r==0) return 0;
s=(a*d+b*e+c*r)-(a*r+d*e+b*c);
if(s<0) s=-s;
printf("%.1f
",s/2.0);
}
return 0;
}
************************************************************************************************
#include<stdio.h> //68 nyist
#include<math.h>
main()
{
int x0,x1,x2,y0,y1,y2;
double s;
while(scanf("%d%d%d%d%d%d",&x0,&y0,&x1,&y1,&x2,&y2))
{
if(x0==0&&y0==0&&x1==0&&y1==0&&x2==0&&y2==0) break;
s=((x1-x0)*(y2-y0)-(x2-x0)*(y1-y0))/2.0;
if(s>=0)
printf("0
");
else
printf("1
");
}
}
************************************************************************
//海伦公式
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int x1,x2,x3,y1,y2,y3;
while(cin>>x1>>y1>>x2>>y2>>x3>>y3)
{
if(x1==0&&x2==0&&x3==0&&y1==0&&y2==0&&y3==0) break;
double a,b,c,p,s;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
p=(a+b+c)/2;
printf("%.1f
",sqrt(p*(p-a)*(p-b)*(p-c)));
}
return 0;
}
#include<stdio.h>
#include<math.h>
int main()
{
int x1,x2,x3,y1,y2,y3;
while(scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3))
{
if(x1==0&&x2==0&&x3==0&&y1==0&&y2==0&&y3==0) break;
double a,b,c,p,s;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
p=(a+b+c)/2;
printf("%.1f
",sqrt(p*(p-a)*(p-b)*(p-c)));
}
return 0;
}
#include<stdio.h>
#include<math.h>
main()
{
int x1,x2,x3,y1,y2,y3,m;
double a,b,c,s,area;
scanf("%d",&m);
while(m--)
{
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
if(x1==0&&y1==0&&x2==0&&y2==0&&x3==0&&y3==0)
continue;
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
c=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
s=1.0/2*(a+b+c);
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("%.1f
",area);
}
}
#include<stdio.h>
#include<math.h>
int main()
{
int x1,y1,x2,y2,x3,y3;
while(scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF&&(x1!=0||x2!=0||x3!=0||y1!=0||y2!=0||y3!=0))
{
double l1,l2,l3,p,s;
l1=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
l2=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
l3=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
p=(l1+l2+l3)/2;
s=sqrt(p*(p-l1)*(p-l2)*(p-l3));
printf("%.1f
",s);
}
return 0;
}