To begin or not to begin
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 783 Accepted Submission(s): 508
Problem Description
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.
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.
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
Source
Recommend
解题思路:
【题意】
箱子里有1个红球和k个黑球
Alice和Bob轮流不放回地从箱子里随机取出一个球
当某人取到红球时获得胜利,游戏结束
问先手是否获胜几率更大,几率更大,输出"1";几率更小,输出"2";几率相等,输出"0"
【类型】
概率水题
【分析】
此题关键还是要多尝试,手动计算一下
当k=1时,一个红球和一个黑球,先手获胜的概率P为
当k=2时,一个红球和两个黑球,先手获胜的概率P为
当k=3时,一个红球和三个黑球,先手获胜的概率P为
故,当k时,先手获胜的概率P为
那我只需判断两者大小关系即可
【时间复杂度&&优化】
O(1)
网上的比较正统的方式
/*Sherlock and Watson and Adler*/ #pragma comment(linker, "/STACK:1024000000,1024000000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<stack> #include<math.h> #include<vector> #include<map> #include<set> #include<list> #include<bitset> #include<cmath> #include<complex> #include<string> #include<algorithm> #include<iostream> #define eps 1e-9 #define LL long long #define PI acos(-1.0) #define bitnum(a) __builtin_popcount(a) using namespace std; const int N = 5005; const int M = 100005; const int inf = 1000000007; const int mod = 1000000007; const int MAXN = 100005; int main() { int k,p; while(~scanf("%d",&k)) { p=k/2+1; if(p>k+1-p) puts("1"); else if(p<k+1-p) puts("2"); else puts("0"); } return 0; }
但是AC代码:
#include<bits/stdc++.h> using namespace std; int main() { int k; while(~scanf("%d", &k)){ if(k%2) printf("0\n"); else printf("1\n"); } return 0; }