`
阿尔萨斯
  • 浏览: 4149888 次
社区版块
存档分类
最新评论

UVA 10167 Birthday Cake(判断点在直线的哪边)

 
阅读更多

UVA 10167 Birthday Cake(判断点在直线的哪边)

题意:

在一个二维平面上,有一个以(0,0)为圆心的圆,现在圆内有很多整点,现在要你找出一条这样直线AX+BY=0,使得所有整点正好在直线的两边。要你输出A和B,且A和B都属于[-500,500]范围。

分析:

由于A和B都是整数且数据范围比较小,所以我们直接枚举A和B的值即可.

一个点如果在AX+BY=0直线上,那么把坐标带入直线值为0.

否则在同一个方向的点的坐标值带入直线方程, 方程的值要么同时>0 要么同时<0. 所以我们只要看点坐标带入直线方程所得的值是否同号就可以判断到底直线两边各有多少个点.

AC代码:

#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=100+5;
struct Point
{
    int x,y;
}P[maxn];

void find(int n)
{
    for(int a=-500; a<=500; ++a)
    {
        for(int b=-500; b<=500; ++b)
        {
            if(a==0 && b==0 )continue;
            int i,cnt=0;//记录使得AX+BY>0的点数目
            for(i=0; i<n; ++i)
            {
                if(a*P[i].x+b*P[i].y>0) ++cnt;
                else if(a*P[i].x+b*P[i].y==0) break;
            }
            if(i<n) continue;
            if(cnt==n/2)
            {
                printf("%d %d\n",a,b);
                return ;
            }
        }
    }
}

int main()
{
    int n;
    while(scanf("%d",&n)==1 && n)
    {
        n*=2;
        for(int i=0;i<n;++i)
            scanf("%d%d",&P[i].x,&P[i].y);
        find(n);
    }
    return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics