hdu1577

一开看了下数据1000,比较小,因为要枚举靠经直线的点,由于闲麻烦于是直接枚举两个点构成的直线所在正方形的所有点,结果超时。感觉数据量并不大。

后来只能枚举靠经直线的点,但是想了想枚举靠近直线的点感觉非常难做到,于是思路反转一下,其实可以没觉对应两点过横坐标之间的值作为x,然后通过直线公式计算出y,现在只在判断y是否是整数就好了,因为如果是整数说明存在这样的整数点在这条直线上。结果就是:如果y不是整数输出Yes 否者输出No. 这个方法本来很容易实现,结果这题卡我的精度,在判断y是否是整数的那个卡了我的精度,坑死了。后来网上看了下原来还有更简单的办法,就是判断dy 和 dx是否存在公约数,想想觉得这个方法的确巧妙。

枚举点法:

/*
* this code is made by LinMeiChen
* Problem:
* Type of Problem:
* Thinking:
* Feeling:
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<list>
using namespace std;
typedef long long lld;
typedef unsigned int ud;
#define oo 0x3f3f3f3f
#define ep 1e-9

int main()
{
    double sx, sy, px, py, L, temp;
    int x, xx;
    bool can;
    while (scanf("%lf", &L) && L!=0.0)
    {
        scanf("%lf %lf %lf %lf", &sx, &sy, &px, &py);
        if (fabs(px) > L || fabs(py) > L)
        {
            puts("Out Of Range");
            continue;
        }
        if (px == sx)
        {
            if (fabs(py - sy) <= 1.0)
                puts("Yes");
            else
                puts("No");
            continue;
        }
        if (py == sy)
        {
            if (fabs(px - sx) <= 1.0)
                puts("Yes");
            else
                puts("No");
            continue;
        }
        can = true;
        x = min(sx, px);
        xx = max(sx, px);
        for (int i = x + 1; i < xx; i++)
        {
            temp = (1.0*i - sx)*(py - sy) / (px - sx) + sy;
            if (temp == (double)(int)temp)
            {
                can = false;
                break;
            }
        }
        if (can)
            puts("Yes");
        else
            puts("No");
    }
    return 0;
}
/*
题目数据:
5 0 0 1 1
5 0 0 2 0
5 0 0 6 6
5 0 0 -1 -1

第一种情况:
5 1 0 2 0
5 1 0 3 0
5 0 1 0 2
5 0 1 0 3

第二种情况:
5 5 5 -5 -5
5 -1 -1 1 1
5 0 0 4 2
10 3 1 6 2
*/
公倍数法:

/*
* this code is made by LinMeiChen
* Problem:
* Type of Problem:
* Thinking:
* Feeling:
*/
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<string>
#include<vector>
#include<queue>
#include<list>
using namespace std;
typedef long long lld;
typedef unsigned int ud;
#define oo 0x3f3f3f3f
#define ep 0.0005

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

int main()
{
    double sx, sy, px, py, L, temp;
    int x, xx;
    bool can;
    while (scanf("%lf", &L) && L != 0.0)
    {
        scanf("%lf %lf %lf %lf", &sx, &sy, &px, &py);
        if (fabs(px) > L || fabs(py) > L)
        {
            puts("Out Of Range");
            continue;
        }
        if (px == sx)
        {
            if (fabs(py - sy) <= 1.0)
                puts("Yes");
            else
                puts("No");
            continue;
        }
        if (py == sy)
        {
            if (fabs(px - sx) <= 1.0)
                puts("Yes");
            else
                puts("No");
            continue;
        }
        int ans = gcd((int)abs(px - sx), (int)abs(py - sy));
        if (ans > 1)
            puts("No");
        else
            puts("Yes");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值