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
  • 相关阅读:
    Android不规则瀑布流照片墙的实现+LruCache算法
    嵌入式OS入门笔记-以RTX为案例:六.RTX的任务调度
    Oracle backgroup processes
    Android中数据库的操作流程详解
    Dreamweaver PHP代码护眼配色方案
    Twitter 新一代流处理利器——Heron 论文笔记之Heron架构
    Docker简单介绍
    C#下使用GDAL
    Android:实现仿 美团/淘宝 多级分类菜单效果
    KVC在定义Model类中的妙用
  • 原文地址:https://www.cnblogs.com/daydayupacm/p/5768757.html
Copyright © 2011-2022 走看看