相同的 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 ;
}
随机推荐
- React Native开发环境搭建
安装Xcode 安装Homebrew 安装Android SDK 安装flow和watchman 安装nodejs 安装react-native-cli 安装Genymotion 安装Webstorm ...
- duplicate symbol _OBJC_METACLASS_$ 报错记录
duplicate symbol _OBJC_METACLASS_$_TabbarButton in: /Users/hw201406/Library/Developer/Xcode/DerivedD ...
- MVC 菜鸟学习记录1
Asp.Net MVC 模式是一种表现模式.它将web应用程序分成三个主要组件即: Model.View.Controller M:Model 主要是存储或者是处理数据的组件 Model其实是实 ...
- log4net保存到数据库系列三、代码中xml配置log4net
园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一.WebConfig中配置log4net 一.WebConfig中配置log4ne ...
- OpenFlow概述
OpenFlow名称里面虽然包括“Flow”,但其并不是“又一个流程引擎”,我们认为世界上的流程引擎已经足够多了,每个流程引擎都各有千秋,有的甚至免费,所以我们不需要再做一个流程引擎,我们做OpenF ...
- 在IIS上发布项目后浏览时报的错:Unable to make the session state request to the session state server
错误描述: Unable to make the session state request to the session state server. Please ensure that the A ...
- C# WinForm 调用WebService
在Winform中对数据库进行操作缺乏安全性,因而可以使用Winform调用WebService来实现对数据库的各种操作. 在VS2010中,创建一个Web服务程序,第一:创建一个空的Web应用程序, ...
- 更改Activity亮度
有些需求需进入到页面后更改Activity的亮度,退出页面后恢复到之前的亮度.通过更改WindowManager.LayoutParams的screenBrightness可以达到这个效果.scree ...
- 解决discuz模板插入jvascript脚本问题(Unexpected token <)
问题描述: 最近学习discuz中要做一个焦点图,添加一个模板,让其显示后台推送图片,其中模块模板中要用到如下的代码 <div class="wrap"> <di ...
- jQuery Ajax应用
jQuery Ajax应用 本章主要了解jQuery的Ajax与传统的Ajax的区别,掌握JQuery的Ajax常用的方法与Ajax相关的函数. 详细内容,请点击jQuery Ajax应用查看: