果园里的树

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

需要注意的是判断两浮点数是否相等时,由于浮点运算的误差,只需判断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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值