Gym 100814A---Arcade Game
2016-04-20 22:09
381 查看
Description
standard input/output
Statements
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.
Sample Input
Input
3
952
925
592
Output
0.000000000
0.166666667
0.194444444
Hint
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.
仅代表个人观点,欢迎交流探讨,勿喷~
PhotoBy:WLOP
http://weibo.com/wlop
standard input/output
Statements
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.
Sample Input
Input
3
952
925
592
Output
0.000000000
0.166666667
0.194444444
Hint
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.
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int n[15]={1,1}; int a[15]; double p,q; int main() { for(int i=2;i<10;i++) { n[i]=n[i-1]*i; } int T; cin>>T; while(T--) { char s[11]; int cnt=0; scanf("%s",s); int l=strlen(s); for(int i=0;i<l;i++) { a[i]=s[i]-'0'; } while(next_permutation(a,a+l)) { cnt++; } if(cnt==0) { printf("0.000000000\n"); } else { double temp=1.0/n[l]; q=p=temp; for(int i=1;i<cnt;i++) { q=p+p*temp; p=q; } printf("%.9lf\n",p); } } return 0; }
仅代表个人观点,欢迎交流探讨,勿喷~
PhotoBy:WLOP
http://weibo.com/wlop
相关文章推荐
- EHcache经典配置
- 冲刺第三天
- VC++判断文件或文件夹是否存在
- C语言初级的小程序
- 01背包,完全背包,多重背包的个人总结
- 深入理解linux网络技术内幕读书笔记(一)--简介
- android获取指定路径下目录文件
- string::npos表示不存在的位置
- 团队项目:个人工作总结02
- Andriod中绘(画)图----Canvas的使用详解
- 【CQOI2016】密钥破解
- 最易忽略的东东:iptables和selinux
- java 继承
- Linux系统调用
- (LeetCode 130)Surrounded Regions(并查集)
- Gson之手动方式
- AlertDialog的简单封装
- C++作业4
- idea快捷键
- linux基础学习之 gsoap calc_test