GYM100814A(利用全排列函数)

4 篇文章 0 订阅
Arcade mall is a new modern mall. It has a new hammer game called "Arcade Game". In this game you're presented with a number n which is hanged on a wall on top of a long vertical tube, at the bottom of the tube there is a button that you should hit with your hammer.

When you hit the button with all your force (as you always do), a ball is pushed all over the tube and hit the number n. The number n flies in the air and it's digits fall back in any random permutation with uniform probability.

If the new number formed is less than or equal to the previous number, the game ends and you lose what ever the new number is. Otherwise (if the number is greater than the previous number), you are still in the game and you should hit the button again.

You win if the new number formed is greater than the previous number and it is equal to the greatest possible permutation number.

Can you compute the probability of winning?

Input

The first line of the input contains the number of test cases T. Following that there are T lines represents T test cases. In each line, there is a single integer (1 ≤ n ≤ 109) the target number. The digits of n are all unique, which means that any 2 digits of n are different.

Output

For each test case, print one line containing the answer. Print the answer rounded to exactly 9 decimal digits.

Example
Input

3
952
925
592

Output

0.000000000
0.166666667
0.194444444

Note

In the first test case, the answer is 0 because 952 is greater than all 2,5 and 9 permutations so you can't win, whatever you do.

In the second test case, the answer is 0.166666667 because you may win by getting number 952 with probability 1/6.

In the third test case the answer is 0.194444444 because you may win by getting number 952 in round1 with probability 1/6 or you can win by getting number 925 in round 1 and then 952 in round 2 with probability 1/6 * 1/6.

题目大概是:求给你的这个数的全排列中大于当前数。本来以为简单的大于的数除以全排列的数就搞定,看了第三个实例的给的解释,发现自己想简单了,除了第一次就拿到最大的除外,还可以拿一个比当前的数值大一点的,然后下一次拿一个更大一点的,直到你拿到最大的那个数了。
自己打全排列肯定超时,毕竟数值到了十的九次方,所以用到了C++STL中全排列函数next_permutation

代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    int  a[15];
    double sum,p;
    while(n--)
    {
        int i=0,x=0,y,big=0,small=0;
        memset(a,0,sizeof(a));
        cin>>m;
        y=m;
        while(m)
        {
            a[i]=m%10;
            m/=10;
            i++;
        }
        sort(a,a+i);
        do
        {
            x=0;
            for(int j=0;j<i;j++)
            {
                x*=10;
                x+=a[j];
            }
            if(x>y)
                big++;
            else if(x<y)
                small++;
        }while(next_permutation(a,a+i));
        if(big==0)
            cout<<"0.000000000"<<endl;
        else
        {
            sum=1.0/(big+small+1);
            p=sum;
            for(int j=0;j<big-1;j++)
            {
                p=p+p*sum;
            }
            printf("%.9lf\n",p);
        }
    }
}

这里求全排列的位数对应种类数可以使用函数求解:

n[0]=0;
n[1]=1;
for(int i=2;i<10;i++)
    {
        n[i]=n[i-1]*i;
    }

大约为:n[15]={0,1,2,6,24,120,720,5040,40320,362880,3628800}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值