LightOJ1104---Birthday Paradox (概率)

    xiaoxiao2025-07-17  5

    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

    题目大意

    给出意个星球上每年的天数,问,在一场宴会上至少出席多少人,他们之中两人同一天生日的概率不低于0.5

    题解;

    常规概率问题,我的做法是反求,求出他们生日不同的概率;

    设共有n天,去了m个人,则不同的概率为:

    (n-1)/n*((n-2)*n))*........*(n-m)/n;

    #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int main() { int t,n,k=1; scanf("%d",&t); while(t--) {double p=1; scanf("%d",&n); printf("Case %d: ",k++); //if(n==1||n==2) //{printf("2\n"); //continue;} for(int i=1;i<=n;i++) {p=p*(double)(n-i)/(double)n; if(p<=0.5) { printf("%d\n",i); break;} } } }另外我感觉此题测试数据有bug,当n为1时,来得人不应该是不是2吗,但所给测试数据明显不对;

    转载请注明原文地址: https://ju.6miu.com/read-1300783.html
    最新回复(0)