A - Arcade Game Gym - 100814A

版权声明:转载请注明出处,谢谢。 https://blog.csdn.net/Mercury_Lc/article/details/88358854

A - Arcade Game

 Gym - 100814A 

&:给一个n,n的每一位数都是不相同的,现在给你一个n,每一次可以随机让n的每一位重新排列,问能够让n排列之后变成最大数的期望。

&:自己忘记了那个生成排列的函数了,于是自己随机写了写,于是就WA了,真的是,还找不到哪里出错了。

&:于是来直接用 next_permutation( ) 来生成全排列了,注意的是,这里生成的全排列是按照给的数的递增来排的。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e5 + 10;
double dp[maxn];
int main()
{
    ios::sync_with_stdio(0);
    int t;
//    s.resize(20);
//    scanf("%d", &t);
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        memset(dp,0,sizeof(dp));        
//        scanf("%s",&s[0]);
        int len = s.length();
        int n = 1;
        for(int i = 1; i <= len; i ++)n *= i;
        int pos = 0;
        while(next_permutation(s.begin(),s.end())) pos++;
        double o = 1;
        dp[0] = 1;
        for(int i = 1; i <= pos; i ++)
        {
            dp[i] = 1.0 /n*o;
            o += dp[i];
        }
        dp[0] = 0;
        printf("%.9lf\n",dp[pos]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mercury_Lc/article/details/88358854
今日推荐