uva_10167_Birthday Cake( 窮舉+卡精度 )

題意:

給你一系列的點,讓你找出一條直線AX+BY=0, 使得直線把這些點劃分爲兩個部分

分析:

由於題目給出的AB範圍小且爲整數,直接暴力可以過,不過這裏要注意精度問題,判斷是否在直線上不能直接與zero比較

Code:

#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

#define oo      (~0u)>>1
#define INF     0x3F3F3F3F
#define REPI(i, s, e)   for(int i = s; i <= e; i ++)
#define REPD(i, e, s)   for(int i = e; i >= s; i --)

const int MAXN = 100;
const int EPS = 10e-6;

double px[MAXN+1], py[MAXN+1];

void input(int n)
{
        for(int i = 1; i <= n; i ++) {
                scanf("%lf %lf", &px[i], &py[i]);
        }
}

int ok(int a, int b, int n)
{
        if( !b ) {
                return 0;
        }
        double k = -a/(double)b;
        int up_cnt = 0, down_cnt = 0;
        for(int i = 1; i <= n; i ++) {
                double c = py[i]-k*px[i];
                if( fabs(c-0) >= -EPS && fabs(c-0) <= EPS ) {
                        return 0;
                }
                else if( c > 0 ) {
                        up_cnt += 1;
                }
                else {
                        down_cnt += 1;
                }
        }
        return up_cnt == down_cnt;
}

void process(int n)
{
        int MIN_VAL = -500, MAX_VAL = 500;
        for(int a = MIN_VAL; a <= MAX_VAL; a ++) {
                for(int b = MIN_VAL; b <= MAX_VAL; b ++) {
                        if( ok(a, b, n) ) {
                                printf("%d %d\n", a, b);
                                return;
                        }
                }
        }
}

int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
        freopen("test.in", "r", stdin);
#endif
        int n;
        while( scanf("%d", &n) && n ) {
                n *= 2;
                input(n);
                process(n);
        }
        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值