【LightOJ 1104Birthday Paradox】

    xiaoxiao2024-12-25  17

    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

    AC代码:

    #include<cstdio> int main() { int T,nl = 0,n; scanf("%d",&T); while(T--){ scanf("%d",&n); int m = n,i; double ans = 1.0; for(i = 1; i <= n; i++){ ans *= 1.0 * m-- / n; // 不在同一天的概率 if(ans < 0.5) break; } if(n == 1 || n == 2) i = 2; printf("Case %d: %d\n",++nl,i - 1); } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1294959.html
    最新回复(0)