UPC 2017 Summer Training 1
来源:互联网 发布:联合智业怎么样知乎 编辑:程序博客网 时间:2024/05/19 03:24
A - Arcade Game
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?
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.
For each test case, print one line containing the answer. Print the answer rounded to exactly 9 decimal digits.
3952925592
0.0000000000.1666666670.194444444
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<stdio.h>#include <algorithm>#include<string.h>using namespace std;int main(){int b[15]={0,1,2,6,24,120,720,5040,40320,362880,3628800};//先将前十个打个表int a[15];char c[15];int n,y,cont;double sum,q,p;while(scanf("%d",&n)!=EOF){while(n--){scanf("%s",&c);cont=0;int l=strlen(c);for(int i=0;i<l;i++){a[i]=c[i]-'0';}do{cont++;}while(next_permutation(a,a+l)); cont--;//这样会包括等于的情况 减去sum=1.0/b[l];if(cont==0)//如果没有比他大的排序printf("0.000000000\n");else{p=q=sum;for(int i=0;i<cont-1;i++)// 此处看下边的解释{p=p+p*sum;}printf("%.9f\n",p);}}}return 0;}PS: 第一次取比给的数大一点的数值,第二次的话就是第一次取的那个数概率*比第一次取的数大一点的概率+直接取这个比第一次大一点的概率~~~~~~~~~~一直到后来,画画,想想就行了~主要是C++的那个函数很给力。
- UPC 2017 Summer Training 1
- UPC 2017 Summer Training 1
- UPC 2017 Summer Training 1
- UPC 2017 Summer Training 1
- UPC 2017 Summer Training 5
- UPC 2017 Summer Training 5
- UPC 2017 Summer Training 6 A,D,I
- UESTC 2017 Summer Training #1 Div.2
- BUPT 2017 summer training (for 16) #1
- 2017 Summer Training Day1
- BUPT Summer training 1 总结
- SDKD 2017 Summer Single Training #01(训练赛1)
- 2017 Xian ACM Summer Training Warm-up Exercise 1
- UESTC 2017 Summer Training #2 Div.2
- E- Refraction----(2015 NEUQ_ACM summer training #1)
- ecjtu-summer training #1 F (UVA 11461)
- Tri_integral Summer Training 6
- BUPT-SUMMER-TRAINING-搜索
- A*算法-路径规划
- 匈牙利算法
- Hibernate的工具类HibernateUtils.java中创建sessionFactory的相关问题
- ContentProvider Hook解析
- jmeter 报告优化
- UPC 2017 Summer Training 1
- AJAX-简单示例
- day08
- 8月2日训练
- 组件化架构漫谈
- 机器学习教程之13-决策树(decision tree)的sklearn实现
- 丑陋的字符串2
- Lucene&&Solr中的域(Filed)总结
- TabLayout、ViewPager和Fragment之间的通讯