Lightoj 1027 - A Dangerous Maze 【期望】
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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 inxi minutes, or can take you out of the maze afterxi 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 containsn space separated integers. If theith integer(xi)
is positive, you can assume that theith door will take you out of maze afterxi minutes. If it's negative, then theith door will take you back to the beginning position afterabs(xi)
minutes. You can safely assume that1 ≤ 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 inp/q format. Where
p is the numerator of the result andq is the denominator of the result and they are relatively prime. See the samples for details.
Sample Input |
Output for Sample Input |
3 1 1 2 -10 -3 3 3 -6 -9 |
Case 1: 1/1 Case 2: inf Case 3: 18/1 |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
using namespace std;
int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a%b);
}
int a[110];
int main()
{
int t;
int N;
int k = 1;
scanf("%d", &t);
while(t--)
{
scanf("%d", &N);
int sum1 = 0, sum2 = 0;
int door1 = 0, door2 = 0;
for(int i = 1; i <= N; i++)
{
scanf("%d", &a[i]);
if(a[i] > 0)
{
sum1 += a[i];
door1++;
}
else
{
sum2 += abs(a[i]);
door2++;
}
}
int up = sum1 + sum2;
int down = N - door2;
printf("Case %d: ", k++);
if(door2 == N)
printf("inf\n");
else
printf("%d/%d\n", up / GCD(up, down), down / GCD(up, down));
}
return 0;
}
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
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) ...
随机推荐
- 经常遇到的js兼容问题大总结----最全总结
001.获取滚动条滚动的距离 var sTop = document.documentElement.scrollTop || document.body.scrollTop 002.获取非行间样式 ...
- vue中的组件传值
组件关系可以分为父子组件通信.兄弟组件通信.跨级组件通信. 父传子 - props 子传父 - $emit 跨级可以用bus 父子双向 v-model 父链(this.$parent this.$ch ...
- PyQt5实现第一个桌面应用程序
import sysfrom PyQt5.QtWidgets import QApplication,QWidget,QDialogfrom PyQt5.QtCore import Qt if __n ...
- 第1节 MapReduce入门:11、mapreduce程序的入门
1.1.理解MapReduce思想 MapReduce思想在生活中处处可见.或多或少都曾接触过这种思想.MapReduce的思想核心是“分而治之”,适用于大量复杂的任务处理场景(大规模数据处理场景). ...
- 怎样让Oracle的存储过程返回结果集
Oracle存储过程: CREATE OR REPLACE PROCEDURE getcity ( citycode IN VARCHAR2, ref_cursor OUT sys_refcursor ...
- 诊断:AWR快照停止自动采集
11.2.0.4数据库中,MMON进程,有时候由于一些莫名其妙的原因挂掉,接下来AWR的快照也就无法正常自动生成.MMON进程应该自动重启,却并没有自动被启动. 那么我们有可能是遇到了bug Bug ...
- jenkins构建项目记录1
jenkins安装见上篇随笔 1.新建任务 2.构建一个自由风格的软件项目 3.源码管理设置 4.构建环境 5.构建 6.构建后操作
- jq ajax请求error: Maximum call stack size exceeded
原因是data中参数iconUrl这个变量未声明导致的.jq在内部循环时报错
- Mac使用Aria2下载百度网盘,突破下载限速的方法教程
百度网盘目前可以说是在国内网盘环境中一枝独秀,日常使用触及在所难免,尤其是对于喜欢下载资源的朋友来说,但是一些限制让它的使用越来越难,尤其是下载速度,普通用户的下载往往远低于自己的预期,特别是对于 M ...
- MySQL-----查
数据库在我眼中就是增删改查,而查,我觉得是数据库最费劲的,数据库的花式查,各种查.下面咱们不废话,就是干. 查: **查数据库版本** select version(); **查登录用户** sele ...