微软2017年预科生计划在线编程笔试题Legendary Items
来源:互联网 发布:摩擦交友软件 编辑:程序博客网 时间:2024/05/16 15:45
题目1 : Legendary Items
- 样例输入
50 75 2
- 样例输出
3.25
描述
Little Hi is playing a video game. Each time he accomplishes a quest in the game, Little Hi has a chance to get a legendary item.
At the beginning the probability is P%. Each time Little Hi accomplishes a quest without getting a legendary item, the probability will go up Q%. Since the probability is getting higher he will get a legendary item eventually.
After getting a legendary item the probability will be reset to ⌊P/(2I)⌋% (⌊x⌋ represents the largest integer no more than x) where I is the number of legendary items he already has. The probability will also go up Q% each time Little Hi accomplishes a quest until he gets another legendary item.
Now Little Hi wants to know the expected number of quests he has to accomplish to get N legendary items.
Assume P = 50, Q = 75 and N = 2, as the below figure shows the expected number of quests is
2*50%*25% + 3*50%*75%*100% + 3*50%*100%*25% + 4*50%*100%*75%*100% = 3.25
输入
The first line contains three integers P, Q and N.
1 ≤ N ≤ 106, 0 ≤ P ≤ 100, 1 ≤ Q ≤ 100
输出
Output the expected number of quests rounded to 2 decimal places.
看了半天才看懂。
题意:每次完成一个没有legendary item的quest,概率会上升Q%(直到获得legendary item);完成legendary item的quest,概率重设为P/(2^I)%
反映到图中就是:每个节点的左节点为实心节点(完成legendary item的),右节点为空节点(未完成legendary item)。每个空节点(假设概率x)的左节点为x+q,实心节点的左节点概率为P/(2^I)% ,(图中每个节点的左右节点之和为100%)。求为了得到n个legendaryitem,需要多少quest
以上图为例,从上往下看,p为50%,n为2时,最左边一直走,概率分别为:50%,25%(2个legendary item),则停止
将整个树看作n层,从底层开始计算。
下面开始计算:自底向上,从最左的25%节点开始,这一点为0.25*(0+1),此时不需往下走(因为已经拿到2个legendary item)
右节点为1-0.25=0.75(former),向下继续走概率为0.25+q=1(如果小于1,可以往下继续走),则概率为0.75*(0+2)。至此两点之和0.25+0.75*2=1.75
25%的父节点为50%,则50%这一点为0.5*(1.75+1),意思是这一点的子节点加上1个level再乘以0.5就是这一点值
再看另外50%(右节点),former为0.5,继续向下走为0.5*(1.75+2),至此两点之和3.25
AC代码:
public class Main {public static void main(String[] args) { Scanner in = new Scanner(System.in); int p = in.nextInt(); int q = in.nextInt(); int n = in.nextInt(); helper(p, q, n);}public static void helper(int p, int q, int n) {List<Integer> list = new ArrayList<Integer>(); list.add(p); double r = 0; for (int i = 1; i < n; i++) { list.add(list.get(i - 1) / 2); } for (int m = n - 1; m >= 0; m--) { int tmp = list.get(m); double one = 0, former = 1; int level = 1; for (int j = tmp; ; j+= q) { if (j > 100) { j = 100; } one += former * ((double)j / 100)* (r + level) ; former *= (1.0 - (double)j / 100.0); level++; if(j == 100) { break; } } r = one; } System.out.print(r);}}
再推荐一个这场笔试的讲解链接,分析的很好:
https://github.com/hiho-coder/msft-2017-online-test-solution/blob/master/README.md
- 微软2017年预科生计划在线编程笔试题Legendary Items
- 微软2017年预科生计划在线编程笔试 题目1 : Legendary Items
- [hihocoder1489][微软2017年预科生计划在线编程笔试][Legendary Items]
- 微软2017年预科生计划在线编程笔试-#1489 : Legendary Items
- 微软2017年预科生计划在线编程笔试第二场B题Diligent Robots
- 微软2017年预科生计划在线编程笔试_上
- 微软2017年预科生计划在线编程笔试,附解题思路
- 【微软2017年预科生计划在线编程笔试第二场 A】Queen Attack
- 微软2017年预科生计划在线编程笔试第二场-#1497 : Queen Attack
- 微软2017年预科生计划在线编程笔试第二场-#1498 : Diligent Robots
- 微软2017年预科生计划在线编程笔试第二场-#1499 : A Box of Coins
- 微软2017年预科生计划在线编程笔试第二场-#1500 : EL SUENO
- 微软2017年预科生计划在线编程笔试-#1490 : Tree Restoration
- 微软2017年预科生计划在线编程笔试-#1491 : Monster Killing
- 微软2017年预科生计划在线编程笔试-#1492 : Parentheses Sequence
- 微软预科生计划skype在线面试题
- 2017微软预科生计划面试(三面全)
- [hihoCoder 1187] Divisors (微软预科生计划-探星夏令营在线测试)
- ZOJ 3710 Friends
- android基础学习_四大件之ContentProvider
- 《算法导论》为什么经典
- 求一个数的约数有多少个
- AS编写sdk并打成jar包供其它APP调用
- 微软2017年预科生计划在线编程笔试题Legendary Items
- 子集生成各算法
- R语言 自定义区间频数频率表
- 利用c#+jquery+echarts生成统计报表(附源代码)
- 一棵树到底能不能结18种果子
- 【译】用Flow编写更好的JavaScript代码
- 极简风登陆注册
- Spring MVC 整合 Freemarker
- JVM调优—jstack用法