Light OJ 1027

题目大意:

你在一个迷宫里,你面前有n个门,你选择门的概率是一样的,每扇门有一个数字k, 加入这个数字是负数,那么这个门会花费你abs(k)分钟后把你带回原点, 假如这个数字是正数,他可以把你带出迷宫,并且花费时间是k.
问把你带出迷宫的预计期望时间是多少?如果无解输出 “inf”,输出结果要求是最简分数的形式。
 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL;
const int INF = 1e9+7;
const int MAXN = 255;
/**
走出来的期望时间 = 需要走次数的期望*平均走一次花费的时间
需要走次数的期望:假设门的总个数是n,可以走出来的门总个数是k,那么我们走出来期望的次数是 n/k.
平均走一次花费的时间: 总时间/n
*/
int gcd(int a,int b)
{
    return b == 0?a:gcd(b,a%b);
}
int main()
{
    int cas = 1, T, n, a[150];
    scanf("%d", &T);

    while(T --)
    {
        scanf("%d", &n);
        int k = 0, sumTime = 0;
        for(int i=0; i<n; i++)
        {
            scanf("%d", &a[i]);
            sumTime += abs(a[i]);
            if(a[i] >= 0)
                k ++;
        }

        printf("Case %d: ", cas ++);
        if(k == 0)
            puts("inf");
        else
        {
            int a = gcd(sumTime, k);
            printf("%d/%d
", sumTime/a, k/a);
        }

    }

    return 0;
}
原文地址:https://www.cnblogs.com/chenchengxun/p/4908392.html