相同的 birthday
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
题意:地球上一年是365天,在一个最聚会上,加上你自己有23个人.... 在这23个人中,有两个生日是同一天的概率超过50%。
现在要求你输入一年的天数,求在聚会上你还要邀请多少人,才可以使,有两个人生日是同一天的概率超过50%..
例如,火星上一年是669天,那么他还要邀请30个人才使得概率超过50%
解题思路:既然是求两个人同一天的生日的概率,那么就是1-P(E)。这里的P(E)表示任何两个人的生日都不相同的概率
1-P(E)=1-(n/n) * (n-1)/n * (n-2)/n *......... * (n-(m-1))/n
这里的m表示m个人.....
代码如下 :
#include <stdio.h>
double birthday(int n)
{
double ans=1.0;
int m=;
for(int i=; ; i++)
{
ans*=(double)(n-i)/n;
m++;
if(1.0-ans>=0.5)
break;
}
return m;
}
int main()
{
int T,N=;
scanf("%d",&T);
while(T--)
{
N++;
int n;
scanf("%d",&n);
int ans=birthday(n);
printf("Case %d: %d\n",N,ans-);
}
return ;
}
随机推荐
- unity中js脚本与c#脚本互相调用
unity中js脚本与c#脚本互相调用 test1.js function OnGUI() { if(GUI.Button(Rect(25,25,100,30),"JS Call CS& ...
- Git 对象
Git 提供了很多方法可以方便地访问 Git 库中的对象: ♦ 采用不分的 SHA1 哈希值.不必把 40 位的哈希值写全,只采用开头的部分(4 位以上),只要不与现有的其他哈希值冲突即可. ♦ 使用 ...
- (转)WIN7更改用户名访问共享文件夹
原文地址: http://ryy8013.blog.163.com/blog/static/71729589201210610533778/ 一直以来,windows7客户端访问windows ser ...
- Oracle基础 物理备份 冷备份和热备份(转)
一.冷备份介绍: 冷备份数据库是将数据库关闭之后备份所有的关键性文件包括数据文件.控制文件.联机REDO LOG文件,将其拷贝到另外的位置.此外冷备份也可以包含对参数文件和口令文件的备份,但是这 ...
- [CAMCOCO][C#]我的系统架构.服务器端.(二)----DATA层
这一层在园子里有很多很多的介绍了,这层写好之后老胡也没多研究,基本上就是参考的园子里大咖们的写法,具体的说明老胡也细说不了了,把接口和思路简单描述一下就好,如果有问题还是那句话,感谢您不吝赐教,老胡这 ...
- c#中文件上传(1)
* * ;//3M picPath = Server.MapPath("........."); HttpFileCollection postfile = Context.Req ...
- Android OpenGL ES(二)----平滑着色
直线或者三角形上的每个片段混合后的颜色可以用一个varying生成.我们不仅能混合颜色,还可以给varying传递任何值,OpenGL会选择属于那条直线的两个值,或者属于那个三角形的三个值,并平滑地在 ...
- 从ASP.NET的web1子界面刷新打开web1的web0父界面
单击web0界面的按钮bt1触发一下代码: protected void btnSave_Click(object sender, EventArgs e) { string parentJs = @ ...
- blogs
http://blogs.msdn.com/b/tess/archive/2008/02/04/net-debugging-demos-information-and-setup-instructio ...
- WAS缓存导致修改文件不生效问题
当用ftp上传部署增量文件,was应用不生效问题解决方法:一. 修改web.xml文件,需要修改以下三个目录下的文件:1. /opt/IBM/WebSphere/AppServer/profiles/ ...