您的位置:首页 > 其它

果园里的树

2016-04-16 14:02 344 查看
该题主要用到了三角形的有向面积的知识,还有行列式的一点知识;



需要注意的是判断两浮点数是否相等时,由于浮点运算的误差,只需判断fabs(a-b)的值小于一个极小值即可,如1e-9.应当尽量避免浮点运算,在规定只保留n位小数的情况下,可以将所有坐标乘以10^n将其转换为整数间的运算。。。(摘自书上= =)

//5.4.3 P84
#include <iostream>
#include <fstream>
#include <cmath>

//#define LOCAL
using namespace std;

struct Point {
double x;
double y;
};

double areaTriangle2(const Point &a, const Point &b, const Point &c) {
double res = a.x * b.y + b.x * c.y + a.y * c.x
- c.x * b.y - a.y * b.x - a.x * c.y;

return res;
}

int main() {
#ifdef LOCAL
ifstream fin("");
ofstream fout("");

#define cin fin
#define cout fout
#endif // LOCAL

Point a, b, c;
while (cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y) {
int cnt = 0;
for (int i = 1; i <= 99; ++i) {
for (int j = 1; j <= 99; ++j) {
Point o = {i, j};
double s = fabs(areaTriangle2(a, b, c));
double s1 = fabs(areaTriangle2(a, b, o));
double s2 = fabs(areaTriangle2(a, c, o));
double s3 = fabs(areaTriangle2(b, c, o));

if (fabs(s1 + s2 + s3 - s) < 1e-9) {
++cnt;
}
}
}
cout << cnt << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: