LIGHT OJ-1104 Birthday Paradox 【组合】

1104 - Birthday Paradox

Time Limit: 2 second(s) Memory Limit: 32 MB

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
Output for Sample Input
2
365
669
Case 1: 22
Case 2: 30

  1. 题意:最少邀请多少人(包括自己)使至少有两个人生日同一天的概率是0.5;
  2. 思路:设至少有x人, 使所有的生日都不在同一天的概率最大是0.5 即N*(N-1)……*(N-x+1) / N^x<=0.5;
  3. 失误:刚开始列式子用成了组合,都怀疑题目了,最后发现用分步乘法计算,还要好好看看组合了;最后竟然题意都看错了,竟然不包括自己,求的是邀请多少人!!!
  4. 代码如下:
#include<cstdio>
using namespace std;

#define esp 1e-9

int main()
{
    int T; double N; int K=0;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf",&N);
        double s=1.0;int  cnt=0;
        while(s<2.0)
        {   
            ++cnt;
            s*=N/(N-cnt+1);

        }
        printf("Case %d: ",++K); 
        printf("%d\n",cnt-1);//不包括自己
    }
    return 0;
}

你可能感兴趣的:(LIGHT OJ-1104 Birthday Paradox 【组合】)