POJ 3668 Game of Lines

如果用set来做的话,这个题就变的非常非常水了,  完全不用考虑精度问题。

用一个结构体存储直线,然后插到集合里,最后输出集合的大小即可。

然后求公约数的时候有个负数问题,后来发现我多虑了。

负数取模
用%运算的时候,首先是两边都是整数,a%b等同于a-(a/b)*b。在VC6和BCB6里,结果的正负号与a相同,例如:-4%6=-1,4%6=4,4%-6=4,-4%-6=-4。

/*
ID: sdj22251
PROG: calfflac
LANG: C++
*/
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#define MAX 100000000
#define LOCA
#define PI acos(-1.0)
using namespace std;
struct line
{
    int x, y;
    bool operator < (const line & a) const // 重载操作符,其实就是写个排序。 如果不重载,结构体是没法往set里面塞的。
    {
        if(a.x == x)
        return y < a.y;
        return x < a.x;
    }
};
int main()
{
#ifdef LOCAL
    freopen("ride.in","r",stdin);
    freopen("ride.out","w",stdout);
#endif
    int n, i, j, x[202], y[202];
    set<line>st;
    scanf("%d", &n);
    for(i = 0; i < n; i++)
    scanf("%d%d", &x[i], &y[i]);
    for(i = 0; i < n - 1; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            line p;
            p.x = x[i] - x[j];
            p.y = y[i] - y[j];
            int cd = __gcd(p.x, p.y); //系统自带的求公约数函数。
            p.x /= cd;
            p.y /= cd;
            st.insert(p);
        }
    }
    printf("%d\n", st.size());
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值