[Hdu-2048] God, God and God

Source: Internet
Author: User

God, God, and God Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission (s): 20794 Accepted Submission (s): 8780

The awards gala for Problem DescriptionHDU 2006 '10 ACM contest has begun!
In an active atmosphere, the organizer held a special lucky draw with generous prizes. The specific requirements for this activity are as follows:

First, all participants put a note with their own name in the lottery box;
Then, after all the notes are added, each person takes a note from the box;
Finally, if you write your own name on the obtained note, "Congratulations, you have won the prize !"

You can imagine the warm atmosphere at that time. After all, the winner's prize is a coveted Twins signature photo! However, just as all the comedy attempts to design often end with a tragedy, no one won the prize in this lottery!

How can this happen to my God, God, and God?

However, don't be excited first. Now the problem arises. Can you calculate the probability of such a situation?

Not counted? Do you also want to end with a tragedy ?!

The first line of Input data is an integer C, indicating the number of test instances, followed by data in Row C. Each line contains an integer n (1

Output for each test instance, please output the percentage of this situation, each instance's Output occupies a row, the results retain two decimal places (rounding), the specific format please refer to sample output.


Sample Input
12

Sample Output
50.00%

Analysis: This is a recursive question.

1. n people, n tickets, n! . What we need to do is from here n! In this case, find out all the errors of n people.

2. Assume that there are n people and the number of wrong rows is f (n). divide them into 1 person and n-1 person. There are three or four situations:

3. Assume that one person takes his own ticket and n-1 does not take his own ticket. Then, this person only needs to exchange a ticket with any person in n-1, n people can be satisfied. So there is (n-1) * f (n-1 ).

4. Assume that one of n-1 people takes their own tickets, and one person takes their own tickets or not, if you exchange a ticket with the person in n-1, the person in n-1 will be able to meet n people's wrong orders. In addition, n-1 people get rid of their own votes, n-2 people must be satisfied with the wrong row. So there is (n-1) * f (n-2 ).

To sum up, f (n) = (n-1) * [f (n-1) + f (n-2)]

When writing a program, you can use a two-dimensional array cases [21] [2]. The array contains 0th columns to store all the arrangements of n people. n! , The array contains 1st columns to store n people's error rows. f (n) = (n-1) * [f (n-1) + f (n-2)]

import java.util.Scanner;public class Main {static long[][] cases = new long[21][2];static {cases[1][0] = 1;cases[1][1] = 0;cases[2][0] = 2;cases[2][1] = 1;for (int i = 3; i < 21; i++) {cases[i][0] = i * cases[i - 1][0];cases[i][1] = (i - 1) * (cases[i - 1][1] + cases[i - 2][1]);}}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();while (n-- != 0) {int count = scanner.nextInt();System.out.printf("%.2f", 100.0 * cases[count][1] / cases[count][0]);System.out.println("%");}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.