Birthday Paradox(期望)

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

題意:給你一年的天數,讓你算至少有多少個人時兩個人生日相同的概率大於等於50%

可以瞭解一下生日悖論

思路:(算了期望都是倒着求的) 一波先算房間所有人生日都不同的概率。第一個人和房間內其他人生日不同的概率爲365/365,第二個是364/365,第三個是363/365…第N個人是365-N+1/365,所以都不同的概率爲N個人的概率相乘。故所求爲1-P>=0.5

代碼

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include<algorithm>
#define N 2050
#define EPS 0.00000000001
using namespace std;
typedef long long ll;
//const int maxx=2100;

int main()
{
	ios_base::sync_with_stdio(false);
	int T;
	scanf("%d", &T);
	int t = 1;
	while (T--)
	{
		int ans = 1;

		int n;
		scanf("%d", &n);
		double p=1.0 ,sum=0;
		for (int i = n-1; i > 0; i--)
		{
			p*=(double) (i*1.0)/ (double)(n*1.0);
			
			if (1-p>=0.5)
			{
				break;
			}
			ans++;

		
		}
		printf("Case %d: %d\n", t++, ans);
	}

	return 0;


}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章