1003.Crashing Balloon

本文介绍了一款名为“气球大战”的游戏裁判算法。该算法用于判断玩家报告的得分是否真实有效,确保游戏的公平性。通过分析玩家提供的得分,判断是否存在撒谎的情况,并最终确定赢家。

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

原题描述:

On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV.   The rule is very simple.  On the ground there are 100 labeled balloons, with the numbers 1 to 100.  After the referee shouts "Let's go!" the two players, who each starts with a score of  "1", race to crash the balloons by their feet and, at the same time, multiply their scores by the numbers written on the balloons they crash.  After a minute, the little audiences are allowed to take the remaining balloons away, and each contestant reports his\her score, the product of the numbers on the balloons he\she's crashed.  The unofficial winner is the player who announced the highest score.

Inevitably, though, disputes arise, and so the official winner is not determined until the disputes are resolved.  The player who claims the lower score is entitled to challenge his\her opponent's score.  The player with the lower score is presumed to have told the truth, because if he\she were to lie about his\her score, he\she would surely come up with a bigger better lie.  The challenge is upheld if the player with the higher score has a score that cannot be achieved with balloons not crashed by the challenging player.  So, if the challenge is successful, the player claiming the lower score wins.

So, for example, if one player claims 343 points and the other claims 49, then clearly the first player is lying; the only way to score 343 is by crashing balloons labeled 7 and 49, and the only way to score 49 is by crashing a balloon labeled 49.  Since each of two scores requires crashing the balloon labeled 49, the one claiming 343 points is presumed to be lying.

On the other hand, if one player claims 162 points and the other claims 81, it is possible for both to be telling the truth (e.g. one crashes balloons 2, 3 and 27, while the other crashes balloon 81), so the challenge would not be upheld.

By the way, if the challenger made a mistake on calculating his/her score, then the challenge would not be upheld. For example, if one player claims 10001 points and the other claims 10003, then clearly none of them are telling the truth. In this case, the challenge would not be upheld.

Unfortunately, anyone who is willing to referee a game of crashing balloon is likely to get over-excited in the hot atmosphere that he\she could not reasonably be expected to perform the intricate calculations that refereeing requires.  Hence the need for you, sober programmer, to provide a software solution.

Input

Pairs of unequal, positive numbers, with each pair on a single line, that are claimed scores from a game of crashing balloon.

Output

Numbers, one to a line, that are the winning scores, assuming that the player with the lower score always challenges the outcome.
Sample Input
343 49
3599 610
62 36

Sample Output
49
610
62

题意还是很好理解的,就是比较这两个人的分数判断谁输谁赢。

大致分析有三种情况 ( 已知A>B )

1. A , B 分数都正常,那么A赢。

2. A B 分数不正常 , B 撒谎 , A 赢。

3. A B 分数不正常, B 没撒谎, B 赢。

正常是指,这两个数可以因式分解为两个不同的因子,即找到其中的A1 , A2 , B1 , B2 使得 A = A1*A2 , B = B1*B2 成立,同时这四个数在1-100之间而且互不相等(因为每个气球数都是唯一的) 比如62=2*31 ,36 = 3*12 ,就属于第一种情况。

那么不正常是指一旦有某个因子大于100 那么这个结果本身就不正确了属于计算错误。如3599 = 1*3599 ,显然不可能有3599这个气球,所以A 撒谎。属于第三种情况。

又或者这四个因子其中有相等的数,如343 和49 , 343 = 7*49 ,而49 = 1*49 (不可能是7*7 ,因为7只能出现一次) ,因子49同时出现两次,那么判断A撒谎, B 赢 。也是第三种情况。

代码里有点混乱的地方就是判断因子的时候,太紊乱了。借鉴了网上的代码、

#include <stdio.h>
int flag1 , flag2 ;
void dfs( int a , int b , int k )
{
    int kk = k ;
    if ( a == 1 && b == 1 )  /* a和b都分解到了最后 */
    {
        flag1 = 1 ;                  /* a没撒谎 */
        return ;
    }
    if ( b == 1 )
        flag2 = 1 ;                  /* b没撒谎 */
    while ( ( a > kk || b > kk ) && k < 100 )
    {                                    /* 找到ab所有各不相同的因子,再从这些因子中搜索,看是否能重新乘出ab */
        kk ++ ;
        if ( a % kk == 0 )
        {
            dfs( a/kk , b , k );
            if ( flag1 )
                return ;
        }
        if ( b % kk == 0 )
        {
            dfs( a , b/kk , k );
            if ( flag1 )
                return ;
        }
    }
}
int main ( )
{
    int num1 , num2 , temp ;
    while ( ( scanf("%d %d",&num1 , &num2 ) ) != EOF )
    {
        flag1 = 0 , flag2 = 0 ;        /* 假设都说的假话 */
        if ( num1 < num2 )
        {
            temp = num1 ;
            num1 = num2 ;
            num2 = temp ;
        }
        else if ( num1 > num2 )
        {
            dfs ( num1 , num2 , 1 );
            if ( flag1 == 0 && flag2 == 1 )
                printf("%d\n",num2 );
            else
                printf("%d\n",num1 );
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值