Description
Sometimes some mathematical results are hard to believe. One of the common problems is the birthday paradox. Suppose you are in a party where there are 23 people including you. What is the probability that at least two people in the party have same birthday? Surprisingly the result is more than 0.5. Now here you have to do the opposite. You have given the number of days in a year. Remember that you can be in a different planet, for example, in Mars, a year is 669 days long. You have to find the minimum number of people you have to invite in a party such that the probability of at least two people in the party have same birthday is at least 0.5.
Input
Input starts with an integer T (≤ 20000), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105) in a single line, denoting the number of days in a year in the planet.
Output
For each case, print the case number and the desired result.
Sample Input
2
365
669
Sample Output
Case 1: 22
Case 2: 30
题意:地球上一年是365天,在一个最聚会上,加上你自己有23个人.... 在这23个人中,有两个生日是同一天的概率超过50%。
现在要求你输入一年的天数,求在聚会上你还要邀请多少人,才可以使,有两个人生日是同一天的概率超过50%..
例如,火星上一年是669天,那么他还要邀请30个人才使得概率超过50%
解题思路:既然是求两个人同一天的生日的概率,那么就是1-P(E)。这里的P(E)表示任何两个人的生日都不相同的概率
1-P(E)=1-(n/n) * (n-1)/n * (n-2)/n *......... * (n-(m-1))/n
这里的m表示m个人.....
代码如下 :
1 #include <stdio.h> 2 double birthday(int n) 3 { 4 double ans=1.0; 5 int m=0; 6 for(int i=0; ; i++) 7 { 8 ans*=(double)(n-i)/n; 9 m++; 10 if(1.0-ans>=0.5) 11 break; 12 } 13 return m; 14 } 15 int main() 16 { 17 int T,N=0; 18 scanf("%d",&T); 19 while(T--) 20 { 21 N++; 22 int n; 23 scanf("%d",&n); 24 int ans=birthday(n); 25 printf("Case %d: %d ",N,ans-1); 26 } 27 return 0; 28 }