lightoj 1027 A Dangerous Maze 期望

设答案为r,cnt为x[i] >=0的个数

那么r = 1/n *  (Σx[i](x[i] >= 0) + ∑(r - x[i])(x[i] < 0))

然后把r移项到一起解方程, 得到r = ∑|x[i]| / cnt,同除gcd。记得特判下x[i]均为负数的情况即可。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 int T,cas,n,tot,gcd,cnt;
 5 int x[110];
 6 int main()
 7 {
 8     for (scanf("%d",&T);T != 0;T--)
 9     {
10         cas++;
11         tot = cnt = 0;
12         scanf("%d",&n);
13         for (int i = 1;i <= n;i++)
14         {
15             scanf("%d",&x[i]);
16             if (x[i] >= 0)
17             {
18                 cnt++;
19                 tot += x[i];
20             }else
21                 tot -= x[i];
22         }
23         if (cnt == 0)
24         {
25             printf("Case %d: inf\n",cas);
26             continue;
27         }
28         gcd = __gcd(tot,cnt);
29         printf("Case %d: %d/%d\n",cas,tot / gcd,cnt / gcd);
30
31     }
32     return 0;
33 }

原文地址:https://www.cnblogs.com/iat14/p/11410053.html

时间: 2024-08-24 01:13:48

lightoj 1027 A Dangerous Maze 期望的相关文章

[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 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 positio

LightOJ 1027 A Dangerous Maze(期望)

https://cn.vjudge.net/problem/LightOJ-1027 题意:有n扇门,每扇门有个时间ti,选择正数的门可以在ti后带你走出迷宫,负数的门会在ti后带你回到起点,然后重新选择,每扇门被选中的概率是一样的,求期望. 思路: 做这种期望的问题必须得列一个方程来求解,不然无限写下去的话算不出. 设走出去的期望的E,对于第三个案例就是 E = 1/3 * 3  + 1/3( 6 + E) + 1/3 (9 + E),(选择第一扇门就直接出去,第二扇门先6分钟回去,然后再花期

LightOJ 1027 A Dangerous Maze 概率期望

题目链接: https://vjudge.net/problem/LightOJ-1027 题目描述: 有N个门, 每个门的选择是等概率的, 如果选择到正数, 我将在正数秒后逃出迷宫, 如果是负数就重新选, 问逃离的期望时间是多少 解题思路: 我这道题犯蠢了, 我认为是.....说不太明白, 看程序吧, 一下子就能看懂, 其中涉及到了一个约分, 但是我没想到就算是long long 也会溢出, 那么我将约分放进每次mul中呢? 也是不行的, 因为都是素数呢, 不是一样会溢出? 自己在半个小时后才

LightOj 1027 A Dangerous Maze【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题意: 你面前有n个门,每个对应一个数字,若为正xi,代表xi分钟后你会从它走出迷宫,负数则说明你会在-xi分钟后回到出发点且失去记忆.求出去的时间的期望. 代码: #include <iostream> #include <stdio.h> #include <math.h> #include <string> #include

LightOJ - 1027 A Dangerous Maze 概率

题目大意:迷宫里面有n扇门,每扇门有相应的值,假设第i扇门的值为xi,如果xi > 0,那么xi分钟后,走该扇门就可以走出迷宫了,如果xi < 0,表示走了该扇门之后,需要abs(xi)分钟后才能回到原点,问走出迷宫的期望是多少 解题思路:假设有k扇门(正值用x表示,负值用y表示)期望是d的话 那么d = 1 / k * (x1 + x2 + x3 + .. xn) + 1 / k * (abs(y1) + abs((y2) + - + abs(ym) + m * d) 表示有1/k的概率选到

light OJ 1027 A Dangerous Maze (期望)

1027 - A Dangerous Maze PDF (English) Statistics Forum 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

Light OJ 1027 - A Dangerous Maze (数学-期望)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1027 题目大意: 一个迷宫, 有n个门,选择一个门花费为|ai|, 如果选择的门是正数, 那么直接走出迷宫, 否则重新回到起始位置.选择每一道门的概率是一样的.求走出迷宫的花费的期望. 解题思路:n个门中正数的门有s个, 那么一次选择出去的概率为s/n, 那么出去需要次数的期望为n/s. 对于每一次选择, 需要花费的平均时间为sum(|ai|)/n, 那么走出迷宫的花费的期望

LightOJ1027---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

Light OJ 1027 - A Dangerous Maze(概率)

题目大意: 你在一个迷宫里,你面前有n个门,你选择门的概率是一样的,每扇门有一个数字k, 加入这个数字是负数,那么这个门会花费你abs(k)分钟后把你带回原点, 假如这个数字是正数,他可以把你带出迷宫,并且花费时间是k. 问把你带出迷宫的预计期望时间是多少?如果无解输出 “inf”,输出结果要求是最简分数的形式. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm>