UVaOJ 10167 - Birthday Cake


——by A Code Rabbit


Description

两个小女孩生日了,但是小气的妈妈只买一个蛋糕,所以你就悲剧了。

蛋糕上有2N个樱桃,你要把蛋糕一刀切成均等的两半,并且樱桃数也平均分。

蛋糕是圆形的,令蛋糕的中心为坐标轴的原点。

输入樱桃的位置。

输出 A,B 使得 Ax+By=0 为切下去那一刀的轨迹。


Types

Brute Force :: Elementary Skills


Analysis

这题的关键其实在于题目的数据范围[-500, 500],并且为整数。

因为直线方程必过(0, 0),所以直线必然可以通过蛋糕的圆心,把蛋糕均分。

因此不必考虑均分蛋糕的问题。

所以只要在定义域里枚举A、B的值,然后判断是否把樱桃平均分即可。

而判断樱桃与直线的关系的方法是:

把樱桃的坐标带入直线方程的左边,大于0则在直线上方,小于0则在直线下方。


Solution

// UVaOJ 10167
// Birthday Cake
// by A Code Rabbit

#include <cstdio>

const int LIMITS = 10000;
const int RANGE = 500;

int n;

struct Point {
    int x, y;
};

Point cherry[LIMITS];

void Enumerate();
bool IsFair(int a, int b);

int main() {
    while (scanf("%d", &n), n) {
        // Inputs.
        for (int i = 0; i < n * 2; ++i) {
            scanf("%d%d", &cherry[i].x, &cherry[i].y);
        }
        // Brute force! And then, outputs.
        Enumerate();
    }

    return 0;
}

void Enumerate() {
    for (int i = -RANGE; i < RANGE; ++i) {
        for (int j = -RANGE; j < RANGE; ++j) {
            if (IsFair(i, j)) {
                printf("%d %d\n", i, j);
                return;
            }
        }
    }
}

bool IsFair(int a, int b) {
    int difference = 0;
    bool cut_cherry = false;
    for (int i = 0; i < n * 2; ++i) {
        if (a * cherry[i].x + b * cherry[i].y > 0) {
            ++difference;
        } else
        if (a * cherry[i].x + b * cherry[i].y < 0) {
            --difference;
        } else {
            // If you cut a cherry into half, two children wouldn't be happy.
            return false;
        }
    }
    if (difference) {
        return false;
    } else {
        return true;
    }
}





下载PDF

参考资料:无


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值