概述
LightOJ1104---Birthday Paradox
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
找不相同的概率,用1减去,一直减下去直到小于0.5,剩下的自然是可能的概率大于0.5了,输出这个时候的n,别忘了减去自己。1的话特判输出1。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
double n,day;
int t,text = 1;
scanf("%d",&t);
while(t--){
n = 0;
scanf("%lf",&day);
if(day == 1){
printf("Case %d: 1n",text++); continue;
}
n = 1;
for(int i=1; i<day; i++){
n = n*(day-i)/day;
if(n<=0.5){
printf("Case %d: %dn",text++,i);break;
}
}
}
return 0;
}
最后
以上就是喜悦苗条为你收集整理的LightOJ1104---Birthday Paradox的全部内容,希望文章能够帮你解决LightOJ1104---Birthday Paradox所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复