zoukankan      html  css  js  c++  java
  • ACM 概率

    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

    解题思路:

    题目大意是给定一年的天数,让我们求出我们参加某一次聚会至少要有多少人才能使至少两个人生日相等的概率至少为0.5(人数考虑自己本身)。我们可以把使至少两个人生日相等的概率至少为0.5  转化为  没有人生日相等的概率至多为0.5,就是小于等于0.5.要考虑他们的生日不相等,第一个人可以在一年的天数x中任选一天,就是x/x;第二个人就只能在x-1天里任选一天,就是x-1/x;第三个人了就只能在x-2天中任选一天,也就是x-2/x.将他们乘起来,只要它们的乘积小于等于0.5,就可以结束了,然后我们就得到了我们需要的人数(注意考虑人数时要考虑自己本身)。

    程序代码:

    #include <iostream>
    using namespace std;
    int main()
    {
    	int n,k=1;
    	cin>>n;
    	while(n--)
    	{
    		int x,i;
    		double p=1;
    		cin>>x;
    		for(i=0;i<=x;i++)
    		{
    			p=p*(x-i)/x;
    			if(p<=0.5)
    				break;
    		}
    		cout<<"Case "<<k++<<": "<<i<<endl;
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    List of all Oracle Server Parameters
    dbcc Trace
    站在巨人的肩膀上 书籍推荐 (zz)
    浅谈JS包装对象
    与 JavaScript 模块相关的所有知识点
    你真的懂Promise吗
    vue动态绑定class与style总结
    vue使用axios实现excel文件下载的实现
    CSRF 与 XSS
    推荐10个很棒的 JS 库
  • 原文地址:https://www.cnblogs.com/xinxiangqing/p/4742307.html
Copyright © 2011-2022 走看看