算生日相同的概率
2015-08-19 21:25
281 查看
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 is669 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
分析:
为了防止溢出,我们就一边乘一边除!算出生日不在同一天的概率,然后再用一减去就好了
ps:50 人的party生日相同概率竟然高达93%!!!!!!
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 is669 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
分析:
为了防止溢出,我们就一边乘一边除!算出生日不在同一天的概率,然后再用一减去就好了
ps:50 人的party生日相同概率竟然高达93%!!!!!!
#include<iostream> using namespace std; int n; int birthday(int x) { double c = 1.0; int m = 0; for (int i = 0;; i++) { c *= (double)(n - i) / n; //防止溢出 m++; if (1.0 - c >= 0.5) break; } return m; } int main() { int T, ans = 1; cin >> T; while (T--) { cin >> n; cout << "Case " << ans++ << ": " << birthday(n) - 1 << endl; //要减去本人 } return 0; }
相关文章推荐
- JQuery validate验证规则
- Google放出Android6.0最后一个预览版以及API23
- Nginx原理和部署
- hdu 1869 六度分离(SPFA算法)
- HBase Default Configuration
- cvc-complex-type.2.4.a错误解决方法
- hdu2833(Floyd)
- NotePad++中JSLint的使用
- 字符串hash函数
- 归并排序 自顶向下实现
- [转]使用 Vagrant 打造跨平台开发环境
- HDU 1264 Counting Squares(哈希表||离散化)
- hdoj 1969 Pie
- 小比赛(CodeForces 560A,UVA 11040,CodeForces 550B,HDU 1856,UVA 1644,CodeForces 560B,HDU 3405,UVA 10820)
- java 设计类时考虑多线程安全
- Myeclipse 10 破解说明
- 使用VS2010连接MySQL
- 数据持久化 将数据写入到本地 文件管理
- USB引脚定义以及导线的颜色定义
- ACM 概率&&动态规划