计算几何初见之2020牛客寒假算法基础集训营第二场C题数三角(现在才初见我也是服了。。)

 第一次见计算几何的题目,其实这题也不是特别明显。

判断钝角三角形用点积判断此外还要注意三点不能共线,用叉积判断一下平行。。

代码用的上交俞勇老师的ACM国际大学生程序设计竞赛算法与实现里面的代码。

#include <iostream>
#include <malloc.h>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
int cmp(double x)
{
    if (fabs(x) < eps)
        return 0;
    if (x > 0)
        return 1;
    return -1;
}
inline double sqr(double x)
{
    return x * x;
}
struct Point
{
    double x, y;
    Point() {}
    Point(double a, double b) : x(a), y(b) {}
    friend Point operator+(const Point &a, const Point &b)
    {
        return Point(a.x + b.x, a.y + b.y);
    }
    friend Point operator-(const Point &a, const Point &b)
    {
        return Point(a.x - b.x, a.y - b.y);
    }
    friend bool operator==(const Point &a, const Point &b)
    {
        return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
    }
    friend Point operator*(const double &a, const Point &b)
    {
        return Point(a * b.x, a * b.y);
    }
    friend Point operator*(const Point &a, const Point &b)
    {
        return Point(a.x * b.x, a.y * b.y);
    }
    friend Point operator/(const Point &a, const double &b)
    {
        return Point(a.x / b, a.y / b);
    }
    double norm()
    {
        return sqrt(sqr(x) + sqr(y));
    }
} p[600];
double dist(const Point &a, const Point &b) // 两点间距离
{
    return (a - b).norm();
}
double dot(const Point &a, const Point &b) // 点积
{
    return a.x * b.x + a.y * b.y;
}
double det(const Point &a, const Point &b) // 叉积
{
    return a.x * b.y - a.y * b.x;
}
bool judge(Point a, Point b, Point c)
{
    if ((det(a - b, a - c)) == 0)
        return false; // 构不成三角形
    if (dot(a - b, a - c) < 0)
        return true;
    if (dot(b - a, b - c) < 0)
        return true;
    if (dot(c - a, c - b) < 0)
        return true;
    return false;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    double x, y;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> x >> y;
        p[i] = Point(x, y);
    }
    int cnt = 0;
    for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
            for (int k = j + 1; k <= n; k++)
                if (judge(p[i], p[j], p[k]))
                    cnt++;
    cout << cnt;
    return 0;
}
发布了68 篇原创文章 · 获赞 24 · 访问量 9753

猜你喜欢

转载自blog.csdn.net/qq_44115065/article/details/104215711