UVA 10167 Birthday Cake

本文探讨了一种使用直线切割蛋糕的方法,确保每个孩子获得相同数量的樱桃。通过数学分析,我们发现如何仅通过枚举特定参数,即可找到满足条件的切割线。方法涉及对坐标系中樱桃位置的分析,以及通过特定参数 ( A ) 和 ( B ) 来定义切割线。此过程结合了几何和代数原理,为解决实际问题提供了一个有趣的数学视角。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分析

对于一条直线Ax+By=0,如果一点(a,b)满足Aa+Bb>0则该点位于该直线上方,满足Aa+Bb<0则该点位于该直线下方,满足Aa+Bb=0则该点位于该直线上。
直接枚举所有可能的AB,只要上方和下方的个数均等于N,枚举结束。

思路

因为如果存在一颗樱桃在这个切割线上,那么一定不能如此切割,所以如果存在一点在该线上,那么可以跳过这组(A,B),适当剪枝。

代码

#include <cstdio>
#define MAX_N 52
int cherries[MAX_N<<1][2];
int N;

void solve()
{
    for (int A = -100; A <= 100; A++)
        for (int B = -100; B <= 100; B++) {
            int u = 0, d = 0;
            for (int i = 0; i < (N<<1); i++)
                if      (A*cherries[i][0] + B*cherries[i][1] > 0) u++;
                else if (A*cherries[i][0] + B*cherries[i][1] < 0) d++;
                else break;
            if (u == N && d == N) {
                printf("%d %d\n", A, B);
                return;
            }
        }
}

int main()
{
    while (scanf("%d", &N), N) {
        for (int i = 0; i < (N<<1); i++)
            scanf("%d%d", &cherries[i][0], &cherries[i][1]);
        solve();
    }
    return 0;
}

题目

Description

Lucy and Lily are twins. Today is their birthday.Mother buys a birthday cake for them. Now we put the cake onto a Descartes coordinate. Its center is at (0, 0), and the cake’s length of radius is 100.

There are 2 N (N is a integer, 1 ≤ N ≤ 50) cherries on the cake. Mother wants to cut the cake into two halves with a knife (of course a beeline). The twins would like to be treated fairly, that means, the shape of the two halves must be the same (that means the beeline must go through the center of the cake) , and each half must have N cherrie(s). Can you help her?

Note: the coordinate of a cherry (x, y) are two integers. You must give the line as form two integers A, B (stands for Ax + By = 0) each number mustn’t in [−500, 500]. Cherries are not allowed lying on the beeline. For each data set there is at least one solution.

Input

The input file contains several scenarios. Each of them consists of 2 parts:
The first part consists of a line with a number N, the second part consists of 2 N lines, each line has two number, meaning (x, y). There is only one space between two border numbers. The input file is ended with N = 0.

Output

For each scenario, print a line containing two numbers A and B. There should be a space between them. If there are many solutions, you can only print one of them.

Sample Input

2
-20 20
-30 20
-10 -50
10 -5
0

Sample Output

0 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值