LightOj-1027 A Dangerous Maze(期望)
You are in a maze; seeing n doors in front of you in beginning. You can choose any door you like. The probability for choosing a door is equal for all doors.
If you choose the ith door, it can either take you back to the same position where you begun in xi minutes, or can take you out of the maze after xi minutes. If you come back to the same position, you can't remember anything. So, every time you come to the beginning position, you have no past experience.
Now you want to find the expected time to get out of the maze.
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of doors. The next line contains nspace separated integers. If the ith integer (xi) is positive, you can assume that the ith door will take you out of maze after xi minutes. If it's negative, then the ith door will take you back to the beginning position after abs(xi) minutes. You can safely assume that 1 ≤ abs(xi) ≤ 10000.
Output
For each case, print the case number and the expected time to get out of the maze. If it's impossible to get out of the maze, print 'inf'. Print the result in p/q format. Where p is the numerator of the result and q is the denominator of the result and they are relatively prime. See the samples for details.
Sample Input
3
1
1
2
-10 -3
3
3 -6 -9
Sample Output
Case 1: 1/1
Case 2: inf
Case 3: 18/1
题解:给你n扇门,每扇门都有一个数,k代表从这扇门走k分钟出去,-k代表从这扇门走-k分钟回到原点,让你求出去的期望;
设期望为d 则有(以样例3位例子):d=(3+6+d+9+d)/3;
参考代码为:
#include<bits/stdc++.h>
using namespace std;
int T,n,a[],sum,cnt; int gcd(int a,int b){ return b==? a:gcd(b,a%b); } int main()
{
scanf("%d",&T);
for(int k=;k<=T;k++)
{
sum=;cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",a+i),sum+=abs(a[i]);
if(a[i]<) cnt++;
}
if(cnt==n) printf("Case %d: inf\n",k);
else
{
cnt=n-cnt;
if(cnt>sum) swap(cnt,sum);
int num=gcd(sum,cnt);
//int num= __gcd(sum,cnt);
printf("Case %d: %d/%d\n",k,sum/num,cnt/num);
}
} return ;
}
LightOj-1027 A Dangerous Maze(期望)的更多相关文章
- LightOJ - 1027 A Dangerous Maze —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1027 1027 - A Dangerous Maze PDF (English) Statistics For ...
- Lightoj 1027 - A Dangerous Maze 【期望】
1027 - A Dangerous Maze PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Y ...
- [LightOJ 1027] A Dangerous Maze
A Dangerous Maze You are in a maze; seeing n doors in front of you in beginning. You can choose any ...
- LightOJ 1027 - A Dangerous Maze(求期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1027 题意:又一个迷宫,有n个门,每个门又一个值num,如果num>0 说明在n ...
- LightOJ 1027 A Dangerous Maze(期望)题解
题意:n扇门,每扇门后都有一个值x,如果x<0会让你等待-x再重新回到这里选择门,x>0你经过x时间就会被传送走,问你被传送走的期望 思路:假设被传送走的期望为E,那么对于x<0来说 ...
- LightOJ 1027 A Dangerous Maze(期望)
https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新 ...
- LightOJ 1027 A Dangerous Maze (数学期望)
题意:你面前有 n 个门,每次你可以选择任意一个进去,如果xi是正数,你将在xi后出去,如果xi是负数,那么xi后你将回来并且丢失所有记忆,问你出去的期望. 析:两种情况,第一种是直接出去,期望就是 ...
- LightOj 1027 A Dangerous Maze【概率】
题目链接:http://www.lightoj.com/volume_showproblem.php? problem=1027 题意: 你面前有n个门,每一个相应一个数字,若为正xi.代表xi分钟后 ...
- LightOJ - 1395 A Dangerous Maze (II) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II) PDF (English) Statistic ...
- LightOJ - 1027 Dangerous Maze 期望
你在迷宫中;开始时在你面前看到n扇门.你可以选择你喜欢的任何门.所有门的选择门的概率是相等的. 如果您选择第i个门,它可以让您回到您在xi(xi小于0)分钟内开始的相同位置,也可以在xi(xi大于0) ...
随机推荐
- (Codeforce)The number of positions
Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say ...
- c/c++求最大公约数和最小公倍数
最大公约数GCD(Greatest Common Divisor) 最常见的求两个数的最大公约数的算法是辗转相除法,也叫欧几里得算法 该算法的c++语言实现如下: #include<iostre ...
- 如何在 PHP 和 Laravel 中使用 Traits
事实上,PHP 作为一门编程语言存在的问题之一,就是你只能使用单继承.这意味着一个类只能从另一个类中继承.例如,可能希望从几个不同的类继承方法,以防止代码重复.在 PHP 5.4 中 一个新的语言特性 ...
- hdu 1068 Girls and Boys (最大独立集)
Girls and BoysTime Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- JAVA继承中子父类的构造方法
首先,构造方法本身会有一个隐式的无参构造(默认): ①不写构造方法,类中的第一行代码事实上有一个默认的无参构造(系统会隐式为你写好) public class Student { private St ...
- ArcGIS Server10.4安装教程
准备内容 安装环境:win10*64位专业版 安装文件:ArcGIS_Server_Ent_Windows_1041_150998.iso 破解文件:Lic10.4.1.ecp #安装Server前, ...
- 【论文阅读】A practical algorithm for distributed clustering and outlier detection
文章提出了一种分布式聚类的算法,这是第一个有理论保障的考虑离群点的分布式聚类算法(文章里自己说的).与之前的算法对比有以下四个优点: 1.耗时短O(max{k,logn}*n), 2.传递信息规模小: ...
- day 32 操作系统、线程和进程(GIL锁)
一.操作系统/应用程序 a. 硬件 - 硬盘 - CPU - 主板 - 显卡 - 内存 - 电源 ... b. 装系统(软件) - 系统就是一个由程序员写出来软件,该软件用于控制计算机的硬件,让他们之 ...
- TensorBoard:可视化学习
数据序列化 TensorBoard 通过读取 TensorFlow 的事件文件来运行.TensorFlow 的事件文件包括了你会在 TensorFlow 运行中涉及到的主要数据.下面是 TensorB ...
- Linux的awk命令详解
awkawk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,默认以空格为分隔符将每行切片,切开的部分再 ...