zoukankan      html  css  js  c++  java
  • Light OJ 1104 Birthday Pardo(生日悖论)

    ime Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
     

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

    2

    365

    669

    Sample Output

    Case 1: 22

    Case 2: 30

    程序分析:此题的大意就是如果一间屋子里有23个,那么至少有两个人的生日相同的概率超过50%,但是如果在火星他们一年是669,这样他们超过50%的概率就是30个人,我们要做的就是输入天数计算概率超过50%至少需要多少人。

    这个题目利用的排序,所以如果数字一大就会益处,我这里的这个代码就比较好的考虑到了这个情况,就是边乘边除。其次一点就是开始我的主函数的是double 型结果是CE最后改成int型才过的。

    程序代码:

    #include<iostream>
    #include<cstdio>
    using namespace std;
    double birthday(int n)
     {
         double ans=1.0;
         int m=0;
         for(int i=0; ; i++)
         {
             ans*=(double)(n-i)/n;
             m++;
             if(1.0-ans>=0.5)
                 break;
         }
         return m;
     }
    int main()
    {
        int T,m,j=0;
            cin>>T;
        while(T--)
        {    j++;
            cin>>m;
            int k;
            k=birthday(m);
            printf("Case %d: %d
    ",j,k-1);
        }
        return 0;
    }
  • 相关阅读:
    Android ExpandableListView的下拉刷新实现
    Android 使用shape来画线
    三种常见的子查询(三十一)
    数据的准备(三十)
    LIMIT语句解析及本章简单回顾(二十九)
    WHERE、ORDER BY、GROUP BY、HAVING语句解析(二十八)
    查询表达式解析(二十七)
    单表的更新UPDATE和删除记录DELETE(二十六)
    插入记录INSERT(二十五)
    修改数据表——修改列定义和更名数据表(二十四)
  • 原文地址:https://www.cnblogs.com/yilihua/p/4741585.html
Copyright © 2011-2022 走看看