Description
As we all know, Hongshu was awarded the title “The Outstanding Student of UESTC”(成电杰出学生). Surprisingly, he found that another outstanding student shares the same birthday with him. However, after carefully thinking, he realised that it is not a “small probability event”. Now, he wants to ask you, for N people, what is probability of that there are at least two people share the same birthday. If two people share the same birthday, the month and day of their birthdays are the same respectively. (365 days in a year in this problem).
Input
The first line of the input contains only one integer T.
Then T lines follow, every line contains only one integer N (1<=N<=1000).
Output
For each test case, output one line. First ,output "Case #C: ", where C is the number of test case, from 1 to T.
Then,output the probability of that there were at least two people share the same birthday for those N people, which should be printed accurately rounded to four decimals.
Then,output the probability of that there were at least two people share the same birthday for those N people, which should be printed accurately rounded to four decimals.
Sample Input
2
2
3
2
3
Sample Output
Case #1: 0.0027
Case #2: 0.0082
思路:
当n==1时,答案为0.0000;
当n>365时,根据抽屉原理可以知道答案为1.0000
当2<=n<=365时,由组合学知识,答案为1-A(365,n)/(365^n)。
Case #2: 0.0082
思路:
当n==1时,答案为0.0000;
当n>365时,根据抽屉原理可以知道答案为1.0000
当2<=n<=365时,由组合学知识,答案为1-A(365,n)/(365^n)。
#include<stdio.h>
int main()
{
int T,i,j,n,mark;
double s;
while(scanf("%d",&T)!=EOF)
{
mark=1;
while(T--)
{
scanf("%d",&n);
printf("Case #%d: ",mark++);
if(n>=365){printf("1.0000\n");continue;}
else if(n==1){printf("0.0000\n");continue;}
else
{s=1;
for(i=0;i<n;i++)
{
s*=(365-i)/365.0;
}
printf("%.4f\n",1.0-s);
}
}
}
return 0;
}
浙公网安备 33010602011771号