zoukankan      html  css  js  c++  java
  • Birthday Paradox lightoj 1104 生日悖论(概率)

    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 is669 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

    题意:求至少有两个人生日在同一天的概率,问你至少邀请多少个人

    分析:看见至少两个字,我们当然想起来需要求两个人都不在同一天生日的概率了。若第一个人生日任选一天,概率为1,第二个人生日选择的概率只能为364/365,第三个人生日选择的概率为363/365......以此类推。。那么概率为1*364/365*363/365...

    注意:问你邀请多少个人,所以最终答案要减1.

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <map>
    #include <queue>
    #include <stack>
    #include <math.h>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    const int maxn = 4007;
    
    typedef long long LL;
    
    
    int main()
    {
        int T, n, k, cnt=1;
        double ans;
    
        scanf("%d", &T);
    
        while(T --)
        {
            scanf("%d", &n);
            k = 0;
            ans = 1;
    
            while(1-ans<0.5)
            {
                ans *= (n-k)*1.0/n;
                k ++;
            }
    
            printf("Case %d: %d
    ", cnt++, k-1);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    Docker Secrets
    Docker swarm 使用服务编排部署lnmp
    Docker Swarm 服务编排之命令
    Docker Swarm应用--lnmp部署WordPress
    How to suppress 'Maybe this is program method' warnings from ProGuard
    ProGuard代码混淆详细攻略
    ProGuard代码混淆技术详解
    Web攻防之XSS,CSRF,SQL注入
    Spring中初始化bean和销毁bean的时候执行某个方法的详解
    数据库事务隔离级别+Spring 声明性事务隔离级别
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5768757.html
Copyright © 2011-2022 走看看