果园里的树排列成矩阵,他们的x和y坐标均是1~99的整数。输入若干个三角形,一次统计每一个三角形内部和边界上共有多少棵树。
样例输入:
1.5 1.5 1.5 6.8 6.8 1.5
10.7 6.9 8.5 1.5 14.5 1.5
样例输出:
15
17
这题尤其要注意的是浮点误差,注意使用小技巧。
#include <iostream>
#include <math.h>
using namespace std;
double get_area2(double x0,double y0,double x1,double y1,double x2,double y2)
{
return fabs(x0*y1+x2*y0+x1*y2-x2*y1-x0*y2-x1*y0);
}
int main()
{
double x0,x1,x2,y0,y1,y2,area;
int i,j,c=0;
cin>>x0>>y0>>x1>>y1>>x2>>y2;
area=get_area2(x0,y0,x1,y1,x2,y2);
for( i = 1 ; i <= 99 ; ++i )
{
for( j = 1 ; j <= 99 ; ++j )
{
double x;
x=get_area2(x0,y0,x1,y1,i,j)+get_area2(x0,y0,i,j,x2,y2)+get_area2(i,j,x1,y1,x2,y2);
if(fabs(area-x)<1e-9)//避免浮点误差
c++;
}
}
cout<<c<<endl;
system("pause");
return 0;
}