当前位置: 代码迷 >> 综合 >> LightOJ1104---Birthday Paradox (简单概率)
  详细解决方案

LightOJ1104---Birthday Paradox (简单概率)

热度:48   发布时间:2024-01-15 06:44:22.0

题目:LightOJ - 1104 

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

题意:

一年n天,那么至少有几个人,可以保证至少两个人同一天生日的概率大于等于0.5?

分析:

转化一下:一年n天,那么至少有几个人,可以保证所有人同一天生日的概率<=(1-0.5)?

m个人生日不在同一天的概率:n/n*(n-1)/n*(n-2).......(n-m+1)/n

枚举人数,判断什么时候到达0.5即可。

最后别忘了-1,除了自己

import java.util.Scanner;public class Main {static int [] a=new int[105];public static void main(String[] args) {Scanner in=new Scanner(System.in);int cas=0;int T=in.nextInt();while(T-->0){cas++;int n=in.nextInt();int m=n;double t=1.0,ans=1.0;int i=2;for( i=2;i<n;i++) {ans=ans*(1.0*(--m)/n);System.out.println(ans);if(ans<=0.5)break;}System.out.println("Case "+cas+": "+(i-1));}System.gc();}}

 

  相关解决方案