第十二届蓝桥杯省赛 —— 直线

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>

using namespace std;

typedef pair<int, int> P;
typedef pair<pair<int, int>, pair<int,int>> PII;

int gcd(int a, int b)
{
    if(b == 0)  return a;
    return gcd(b, a % b);
}

set<PII> s;

int main()
{
    for(int x1 = 0; x1 < 20; x1 ++ )
        for(int y1 = 0; y1 < 21; y1 ++ )
            for(int x2 = 0; x2 < 20; x2 ++ )
                for(int y2 = 0; y2 < 21; y2 ++ )
                {
                    if(x1 == x2 && y1 == y2)    continue;
                    if(x1 != x2 && y1 != y2)    //避免斜率不存在的情况,和平行的情况
                    {
                        int k1 = y2 - y1, k1f = 0;  //k的分子,kf1记录k1的正负,我们纵向把符号放到分子上    
                        if(k1 < 0)  k1f = 1;
                        int k2 = x2 - x1, k2f = 0;
                        if(k2 < 0)  k2f = 1;
                        int c = gcd(abs(k1), abs(k2));

                        k1 /= c;
                        k2 /= c;

                        int b1 = y1 * (x2 - x1) - x1 * (y2 - y1), b1f = 0;
                        if(b1 < 0)  b1f = 1;
                        int b2 = x2 - x1, b2f = 0;
                        if(b2 < 0)  b2f = 1;
                        c = gcd(abs(b1), abs(b2));

                        b1 /= c;
                        b2 /= c;

                        s.insert(PII(P(pow(-1, k1f + k2f) * abs(k1), abs(k2)), P(pow(-1, b1f + b2f) * abs(b1), abs(b2))));
                    }
                }

    cout << s.size() + 20 + 21 << endl;

    return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值