果园里的树

来源:互联网 发布:java难点在哪 编辑:程序博客网 时间:2024/04/24 07:06

该题主要用到了三角形的有向面积的知识,还有行列式的一点知识;
这里写图片描述00

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

//5.4.3 P84#include <iostream>#include <fstream>#include <cmath>//#define LOCALusing 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;}
0 0
原创粉丝点击