LightOJ-1027 A Dangerous Maze Probability
There are n doors in the maze, and each door has a corresponding value. Suppose the value of the I-th door is xi. If xi is greater than 0, after xi minutes, you can leave this door to walk out of the maze. If xi <0, it indicates that after walking this door, it will take abs (xi) minutes before returning to the origin, what are the expectations of getting out of the maze?
Solution: Assume that there are k doors (positive values are represented by x, negative values are represented by y) and d is expected.
Then d = 1/k * (x1 + x2 + x3 + .. xn) + 1/k * (abs (y1) + abs (y2) +... + Abs (ym) + m * d)
It indicates that the probability of 1/k is selected to any door. It takes x minutes to go to the right door. after going to the main door, you can directly go out. Therefore, the expected value is 1/k * x.
If it is a negative number, it takes y minutes to return to the origin point after y minutes. Therefore, the expectation is 1/k * (y + d)
#include
#include
#include
using namespace std;const int N = 110;const double esp = 1e-5;int num[N];int n, cnt;int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a);}void solve() { int t = 0; for(int i = 0; i < n; i++) t = t + (num[i] > 0 ? num[i] : -num[i]); int g = gcd((n - cnt), t); printf("%d/%d\n", t / g, (n - cnt) / g);}int main() { int test, cas = 1; scanf("%d", &test); while(test--) { scanf("%d", &n); cnt = 0; for(int i = 0; i < n; i++) { scanf("%d", &num[i]); if(num[i] < 0) cnt++; } printf("Case %d: ", cas++); if(cnt == n) { printf("inf\n"); continue; } solve(); } return 0;}