标签:
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,因为题目中给出的案例已经说明不包括作者本人,在第一组案例中按照公式求出的答案本来应该是23,但是输出的却是22,所以答案应该注意要减去1;还有一点需要注意的是题目中说的是至少有两人在同一天生日的概率要超过0.5,所以当概率等于0.5时还不能停下来,要继续下一组,我刚开始做的时候没有注意到这个问题所以错了一次,在while中的条件应为>0.5,而不是》=0.5.
程序代码:
#include<stdio.h>
int main()
{
int t,i,f=0;
double n,v,s;
scanf("%d",&t);
while(t--)
{
f++;
v=1;
scanf("%lf",&n);
s=n;
i=0;
while(v>0.5)
{
v=v*(s/n);
s--;
i++;
}
printf("Case %d: %d\n",f,i-1);
}
return 0;
}
2015 HUAS Summer Trainning #6~F
标签:
原文地址:http://www.cnblogs.com/chenchunhui/p/4739684.html