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;
    }

        

  • 相关阅读:
    A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。
    Fliptile 开关问题 poj 3279
    Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
    Aggressive cows 二分不仅仅是查找
    Cable master(二分题 注意精度)
    B. Pasha and String
    Intervals poj 1201 差分约束系统
    UITextField的快速基本使用代码块
    将UIImage转换成圆形图片image
    color转成image对象
  • 原文地址:https://www.cnblogs.com/Lynn0814/p/4743504.html
Copyright © 2011-2022 走看看