[Explanations] - hdu God, and God [God] wrong row

God, God and God

HDU 2006'10 ACM contest's grand awards show began!
For active atmosphere, the organizers held a spectacular, huge raffle prizes, the specific requirements of this activity is as follows:
First, all staff to the party will write a note with his name into the draw box;
then until all note the addition was complete, each taking a note from the box;
and finally, a note written on if that made his name, then "! Congratulations, you won the lottery,"
you can imagine the warm atmosphere of the time, after all prize winners is that we dream of Twins autographed photos of it! However, as all attempts to design comedy often ends in tragedy, this is not even a raffle last person winning!
My God, God and the good God, how could this happen?
However, the first not to excited, now the question is, can you calculate the probability of it happening?
It does not count? Do you also want to end in tragedy? !

Input

The first line of input data C is an integer, indicates the number of test cases, then the C line data, each line contains an integer n (1 <n <= 20), indicates the number of participants in the lottery.

Output

For each test case, output a percentage of this happens, the output of each row for instance, the results of two decimal places (rounded), refer to the specific format sample output.

Sample Input

1
2

Sample Output

50.00%

AC Code

#include <stdio.h>
int main(void)
{
    int c, n, i;
    double arr[25], sum;
    scanf("%d",&c);
    while(c --){
        scanf("%d",&n);
        arr[0] = 0;
        arr[1] = 0;
        arr[2] = 1;
        for(i = 3; i <= 20; i ++){
            arr[i] = (i - 1) * (arr[i - 1] + arr[i - 2]);
        }
        sum = 1;
        for(i = 1; i <= n; i ++){
            sum *= i;
        }
        printf("%.2f%%\n",(arr[n] / sum) * 100);
    }
    return 0;
}

Thinking

The first idea: this relatively difficult problem, first think about when no one is winning, there are n individual lottery, a total of f [n] species pumping method, if the n-th individual pumping less than the n-th, a total of n- pumping one kind of method, such as the k-th pumping person, then the next there are two cases: the first one is able to get the k-th individual n-th, then the remaining n-2 individuals, methods F [n-2 ] species, if not able to get the k individual n-th, then the remaining total of f [n-1] kind of pumping method. In summary, f [n] = (n-1) * (f [n-1] + f [n-2]), readily available f [1] = 0, f [2] = 1, f [3] = 2; for all permutations, a total of individual n-g [n] = n ratio method, as occurs on the f [n] / [n] g!;

The second idea: When you assume the person i, i-1 if the former were to take the wrong individual, that person i just put their case and who also still meet all wrong, so + a [i-1] (i-1), i-1 if the individual does not satisfy all wrong situation, that one is holding its own, and the remaining i-2 to meet the individual all wrong circumstances, that the first person i i-1 personal exchange can, and the person holding their votes may be any of a i-1, so a + [I-2] (i-1). Then divided by the total number of cases * 100 percent can be replaced

Published 34 original articles · won praise 2 · Views 899

Guess you like

Origin blog.csdn.net/Kapo1/article/details/103541672