计蒜客暑假集训第一阶段第四场 d题

Duha decided to have a trip to Singapore by plane.

The airplane had nn seats numbered from 11 to nn, and nn passengers including Duha which were also counted from 11 to nn.The passenger with number ii held the ticket corresponding to the seat with number ii, and Duha was the number 11 passenger.

All passengers got on the plane in the order of their numbers from 11 to nn.However, before they got on the plane Duha lost his ticket (and Duha was the only passenger who lost the ticket), so he could not take his seat correctly.He decided to take a seat randomly.And after that, while a passenger got on the plane and found that his/her seat has been occupied, he/she selected an empty seat randomly as well.A passenger except Duha selected the seat displayed in his/her ticket if it had not been occupied by someone else.

The first problem you are asked to calculate in this problem is the probability of the last passenger to get on the plane that took his/her correct seat.

Several days later, Duha finished his travel in Singapore, and he had a great time.

On the way back, he lost his ticket again.And at this time, the airplane had mm seats numbered from 11 to mm, and mm passengers including Duha which were also counted from 11 to mm.The passenger with number ii held the ticket corresponding to the seat with number ii, and Duha was the number 11 passenger as well.

The difference was that: all passengers got on the plane in a random order (which was any one of the m!m!different orders with the same chance).Similarly, Duha or a passenger who found his/her seat had been occupied selected an empty seat randomly.

The second problem you are asked to calculate in this problem is the probability of the last passenger to get on the plane that took his/her right seat on the return trip.

Input Format

The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.

For each test case, a line contains two integers nn and m~(1\le n, m\le 50)m (1≤n,m≤50).

Output Format

For each test case, output a line containing Case #x: y z, where xx is the test case number starting from 11, yy is the answer of the first problem, and zz is the answer of the second problem.Both of yy and zz are rounded to 66places, and we guarantee that their 77-th places after the decimal point in the precise answer would not be 44 or 55.

样例输入

1
2 3

样例输出

Case #1: 0.500000 0.666667

题目大意:

问题一:飞机上一共有n个座位,每个座位都有自己的编号,一共有n个乘客,杜哈就是其中之一,每个乘客都有一个票,用来找自己所在的位置,但是杜哈的票丢了,所以他只好上飞机后随便做,原先位置的主人发现自己的位置被别人占了后,他也只好随便坐,杜哈是第一个上飞机的。问,最后一个上飞机的乘客能够做到自己的座位的概率是多少?

问题二:题干和问题一相同,唯一不同是这次杜哈不是第一个上飞机的人了,问,最后一个上飞机的乘客能够做到自己的座位的概率是多少?

计算结果保留到小数点后6位。

思路:

这是一道数学题,只要你们能够总结出规律推导出公式,那么这个题就解决的。

第一问:最后一人能够做到自己座位上的概率恒为1/2。

第二问:最后一人能够做到自己座位上的概率为(m+1)/2m。

具体如何推出来的,原谅我数学不好。

代码:

#include<bits/stdc++.h>
using namespace std;
int t;
int n,m;
int main()
{
    cin>>t;
    int z=1;
    while (t--)
    {
        cin>>n>>m;
        if(n==1)//第一问中的特殊情况
        {
            cout<<"Case #"<<z++<<": ";
            cout<<"1.000000"<<" ";
        }
        if(n!=1)//第一问的正常情况
        {
            cout<<"Case #"<<z++<<": ";
            cout<<"0.500000"<<" ";
        }
        double f=((m+1)*1.0)/(2*m);//计算第二问
        printf("%.6f\n",f);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40938077/article/details/81239569