相同的 birthday
2015-08-18 19:26
387 查看
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个人.....
代码如下 :
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=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,N=0; scanf("%d",&T); while(T--) { N++; int n; scanf("%d",&n); int ans=birthday(n); printf("Case %d: %d\n",N,ans-1); } return 0; }
相关文章推荐
- 中文ubuntu里用户目录里的路径改成英文
- MySQL备份
- 设计模式交流有感
- 贪心算法专题小结——区间相关问题
- 解决Dialog 消失,输入法不消失的问题
- Activity生命周期
- IOS-TextField控件详解
- 将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:
- 自定义异常类一
- fanqiang
- python视频教程大全集下载啦!超级全的教程!
- Dream------spark--spark集群的环境搭建
- Fragment 实现tab页卡切换并保存界面状态,动态添加Fragment
- jQuery 从input中读取的内容的类型
- 【ODPS】本地数据库数据批量上传到ODPS中
- java请求https
- 使用tinyxml解析XML配置文件
- hdu4109(拓扑排序,dp)
- android客户端从服务器端获取json数据并解析的实现代码(重要)
- 给phpcms v9加入一个主题radio无线电button,它可反复使用,以创建不同的专题部分内容编辑器,添加一个主题来定义自己的领域