HDU5978

题意:

A box contains black balls and a single red ball. Alice and Bob draw balls from this box without replacement, alternating after each draws until the red ball is drawn. The game is won by the player who happens to draw the single red ball. Bob is a gentleman and offers Alice the choice of whether she wants to start or not. Alice has a hunch that she might be better off if she starts; after all, she might succeed in the first draw. On the other hand, if her first draw yields a black ball, then Bob’s chances to draw the red ball in his first draw are increased, because then one black ball is already removed from the box. How should Alice decide in order to maximize her probability of winning? Help Alice with decision.

Input

Multiple test cases (number of test cases≤50), process till end of input.
For each case, a positive integer k (1≤k≤10^5) is given on a single line.

Output

For each case, output:
1, if the player who starts drawing has an advantage
2, if the player who starts drawing has a disadvantage
0, if Alice’s and Bob’s chances are equal, no matter who starts drawing
on a single line.

Sample Input

1
2

Sample Output

0
1

​ 翻译成汉语,大概意思是一个箱子中有n个蓝球,1个红球。爱丽丝先抓,鲍勃后抓,直到抓到红球的人获胜为止。问谁赢的概率大。0代表概率一样,1代表爱丽丝赢的概率大,2代表鲍勃赢的概率大

思路:

​ 当n为1的时候,爱丽丝赢的概率是1 / 2;当n为2的时候,爱丽丝赢的概率是1 / 3 + 2 / 3 ✖️ 1 / 2 = 2 / 3;当n为3的时候,爱丽丝赢的概率是1 / 4 + 3 / 4 ✖️ 2 / 3 ✖️1 / 2 = 1 / 2;当n为4的时候,爱丽丝赢的概率是1 / 5 + 4 / 5 ✖️ 3 / 4 ✖️1 / 3 + 4 / 5 ✖️ 3 / 4 ✖️ 2 / 3 ✖️ 1 / 2 = 3 / 5;

扫描二维码关注公众号,回复: 3663711 查看本文章

​ 因此我们发现,当n为偶数的时候,爱丽丝赢的概率大,输出1。当n为奇数时,两者赢的概率相同,输出0。

代码:

#include <stdio.h>
int main () {
    int a;
    while (scanf("%d", &a) == 1) {
        if(a & 1) {
            printf("0\n");
        } else {
            printf("1\n");
        }
    }
    return 0;
}

转载请注明出处!!!

如果有写的不对或者不全面的地方 可通过主页的联系方式进行指正,谢谢

猜你喜欢

转载自blog.csdn.net/Ivan_zcy/article/details/83211026
hdu