Birthday Paradox

来源:互联网 发布:选购笔记本 知乎 编辑:程序博客网 时间:2024/05/17 00:59
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人的聚会,包括你在内。 党内至少有两个人有相同生日的概率是多少? 令人惊讶的是,结果超过0.5。 现在你必须做相反的事情。 你给了一年的天数。 记住,你可以在一个不同的星球,例如在火星,一年是669天。 你必须找到你在一个聚会中邀请的最少人数,使得聚会中至少有两个人的同一生日的概率至少为0.5。

输入
输入以整数T(≤20000)开始,表示测试用例数。
每个案例在一行中包含整数n(1≤n≤105),表示地球上一年中的天数。
输出
对于每种情况,打印案例编号和所需的结果。
样品输入
2
365
669
样品输出
案例1:22
病例2:30



#include<cstdio>  #include<cstring>  int main() {      int T;      scanf("%d",&T);      int c=1;      while(T--) {          int n,ans=1;          scanf("%d",&n);          double p=1;    for(int i=n-1; i>=0; i--) {              p=p*(i*1.0)/(n*1.0);              if(p<=0.5) {                  break;              }              ans++;           }          printf("Case %d: %d\n",c++,ans);     }      return 0;  }   




原创粉丝点击