LightOJ - 1027 A Dangerous Maze 概率

来源:互联网 发布:常见的网络攻击手段有 编辑:程序博客网 时间:2024/05/22 17:17

题目大意:迷宫里面有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的概率选到任意一扇门,走到值为正的门需要x分钟,走到正门后就可以直接出去了,所以期望就为1/k * x
如果是负数的话,需要走y分钟,y分钟后回到原点,就要加上期望了,所以期望就是1/ k * (y + d)

#include<cstdio>#include<cmath>#include<cstdio>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;}
0 0
原创粉丝点击