zoukankan      html  css  js  c++  java
  • 算生日相同的概率

    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

        分析:

             为了防止溢出,我们就一边乘一边除!算出生日不在同一天的概率,然后再用一减去就好了

              ps:50 人的party生日相同概率竟然高达93%!!!!!!

    #include<iostream>
    using namespace std;
    int n;
    int birthday(int x)
    {
        double c = 1.0;
        int m = 0;
        for (int i = 0;; i++)
        {
            c *= (double)(n - i) / n;    //防止溢出
            m++;
            if (1.0 - c >= 0.5)
                break;
        }
        return m;
    }
    int main()
    {
        int T, ans = 1;
        cin >> T;
        while (T--)
        {
    
            cin >> n;
            cout << "Case " << ans++ << ": " << birthday(n) - 1 << endl;      //要减去本人
        }
        return 0;
    }

        

  • 相关阅读:
    Django中多表的增删改查操作及聚合查询、F、Q查询
    路由控制
    路由、视图、模板主要知识点回顾
    创建文件linux
    关于shell,环境变量放置在?
    snprintf和string操作函数
    C/C++的存储区域的划分
    001
    SAMBA服务器
    归并排序原理详解!
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4743504.html
Copyright © 2011-2022 走看看