The 16th Beijing Normal University Programming Contest Finals-Replay-C-Mengmengda Height Difference

Topic description 

"There is a lot of rain during the Qingming Festival, and pedestrians on the road want to break their souls."

However, wfy's mood is happy, because the BNU ACM team is going out for a spring outing! And, um. . .

The following is the diary of wfy classmates:

Yesterday, Mr. He told us: Tomorrow we will go to the spring outing, everyone is ready to drink and eat!
Everyone was excited when they heard it, some cheered, some applauded, and they were extremely happy. The next day, we arrived at the school early, couldn't wait to get in the car, and came to the park. As soon as you walk in, it's so beautiful! There are so many trees in the park, tall and short, thick and thin, and dense, swaying gently in the spring breeze, as if welcoming us. There are so many flowers in the park, red and yellow, purple and white, with a faint fragrance that makes us all drunk. There is a clear river at the corner of the park. The river flows slowly. You can see the fish in the water swimming happily. How comfortable! The water plants are green and green, how fresh! Next to the river is a small forest, looking at a bright green from a distance. We ate, played games, played hide-and-seek, and went crazy in there. Behind the woods is a rolling hillside, winding like a swimming snake. Of course, I think the sky in the park is beautiful too. It is cloudless, clear and clear. The birds are spreading their wings and flying. They have different shapes. They rise for a while, slide down for a while, eat insects for a while, and rest in the woods for a while, very leisurely. Happy times are always so short, and soon, it gets dark. We reluctantly got in the car and went back to school, and I really hope to see this beautiful park again next spring.
After returning to school, Mr. He said: Please form a line, let's take pictures!
Mr. He especially likes cute things, such as **, such as ****, and so on.
Teacher He believes that when the students stand in a row, the more the height difference between the two adjacent students, the cuter the two students stand together.
Then the greater the height difference between all two adjacent classmates, the more adorable the photo, which is the cuteness index of this photo.
Mr. He hopes that the cuteness index of the photos taken is as large as possible.
However, Mr. He is not a math teacher, but a Chinese teacher. Teacher He feels very GG.
Teacher He just wanted to know what the mathematical expectation of the Mengli Index would be if the students were allowed to stand in a random line (all lines are equally likely to stand).
Smart I figured out the answer at once, and then Teacher He rewarded me with a cute gift.
Really happy today.

There are n students in the BNU ACM team, and their heights are respectively . Can you calculate the math expectations that Teacher He wants?

Enter description:

The first is a positive integer T (T ≤ 20), indicating the number of groups of test data,
Each set of test data has only one row and contains an integer n (2 ≤ n ≤ 100).

Output description:

 
  

For each set of test data, output a line, including a real number, which represents the mathematical expectation of the Momentum Index, and the relative error is required not to exceed , that is to say, let the output result be a, the standard answer is b, if it is satisfied , the output result will be considered the correct answer.

Example 1

enter

2
2
3

output

1.000000000000
2.666666666667

illustrate

For the second set of examples, all possible permutations are [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2 ],[3,2,1], so the answer is

Idea 1: We can use the watch to find the pattern (suddenly found that finding the pattern is a good way), when we find 3, 8 / 3; 4, 15 / 3; 5, 24/3; 6, 35 / 3, with And so on when n is ((n * n) - 1) / 3.


#include <iostream>
#include <stdio.h>
using namespace std;
intmain()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        double years = ((n*n)-1.0)/3.0;
        printf("%.12f\n",ans);
    }
    return 0;
}

思路二:由于是全排列,对于n个数,总共有n*(n-1)/2种形式的差,且每种差出现次数相同,两重for循环即可求出所有形式的差出现一次的和sum,那么求出每种差出现的次数就可以求出差的总和。由于每一种顺序有n-1种差,有n!种序列,所以每次出现的次数为:n!*(n-1)/(n*(n-1)/2)=2*(n-1)!所以和即为:(sum*2*(n-1)!)/n! = sum*2/n即为答案。

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        long long sum=0;
        scanf("%d",&n);
        for(int i = n; i >= 1; i--)
        {
            for(int j = 1; j <= n; j++)
            {
                if(i > j)
                sum += (i - j);
            }
        }
        double ans = (2.0*sum)/n;
        printf("%.12f\n",ans);
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325960766&siteId=291194637