Draw card game~2021.1.12

Title description

The inspiration for this question comes from a classical probability model.
Alice draws cards in a card pool, which contains x s cards and y a cards.
Alice randomly draws a card from the card pool without putting it back.
Bob watches Alice draw cards and predicts the result each time:
If there are more s cards in the card pool than a cards, Bob will guess that Alice draws s cards.
Otherwise, it will guess that Alice draws card a.
But if the two cards in the card pool are equal in number, Bob will not make any guesses about the result of the card draw.
Alice will continue to draw cards until the card pool is empty.
Now tell you the number of s-cards and a-cards in the card pool at the beginning. Can you calculate how many times Bob expects to guess correctly?

Input format

Give two integers a, b in one line (1≤a,b≤10 5 )

Output format

A real number represents expectations, rounded to the nearest two decimal places.

Input sample

1 1

Sample output

1.00

Sample explanation

In the initial situation, Bob does not make any guesses. After the first draw, Bob can guess right no matter which card is left in the second draw, so the expectation is 1.00.

AC code

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
    
    
    int a,b;
    cin>>a>>b;
    cout<<fixed<<setprecision(2)<<float(max(a,b));
    return 0;
}

Explanation

① In a 30-point question in PTA, the code looks short, concise, and very simple. It only needs to output the maximum value of the input data according to the weight condition, but the mathematical principles behind it are somewhat complicated.
②Mathematical principle: In fact, it is the application of classical probability model.
Let E(a,b) denote the mathematical expectation stated in the question.
Therefore, E(1,1) = 1, E(2,2) = 2/2 * E(2,1) = 2/2 * (2/3 + 2/3 * E(1,1) + 1 /3 * E(2,0)), after induction, the general formula is obtained: E(a,b) = a/(a+b) + a/(a+b) * E(a-1,b) + b/(a+b) * E(a,b-1) (when a>b, there must be E(a-1,b) = a-1, E(a,b-1) = a), Simplify to E(a,b) = a * (a+b)/(a+b) = a. (The proof method when a = b, a <b is the same as the above, so I won’t repeat it)

  • How to understand E(a,b) = a/(a+b) + a/(a+b) * E(a-1,b) + b/(a+b) * E(a,b-1)?
    As mentioned above, E(a,b) represents the number of correct guesses when Bob draws cards when there are a and b cards in card a and card b, until all cards are drawn. a/(a+b)Indicates the probability that Bob predicts that Alice will draw cards (Bod predicts that Alice draws more cards, and E(a,b) = a/(a+b) + a/(a+b) * E(a-1,b) + b/(a+b) * E(a,b-1)the prerequisite is hypothesis a>b), a/(a+b) * E(a-1,b) + b/(a+b) * E(a,b-1)refers to the true probability of drawing cards, the probability of drawing more cards is a/(a+b) * E(a-1,b), and the probability of drawing fewer cards is b/(a+b) * E(a,b-1).

③ Therefore, as long as the larger of a and b is output according to the conditions, it is the answer.

Guess you like

Origin blog.csdn.net/fatfairyyy/article/details/112515212