LightOJ - 1027 (期望基礎)

LightOJ - 1027 (期望基礎)

題目鏈接:LightOJ - 1027

題目大意: 給你n個門,每次進每個門的概率都是一樣的,正數代表你x分鐘後可以離開這裏,負數代表你x分鐘後回到這裏。求預期離開的時間, 如果時間無限大,輸出inf。
數據範圍: 1abs(xi)10000
解題思路:

E 表示離開所需要的時間。考慮兩種情況:

  • 選了正數, 概率爲1n , 期望爲1kni , 結束。
  • 選了負數, 概率爲1nxi 分鐘之後回到原點, 那麼期望爲1n(E+xi)

如果正數個數爲0, 答案爲inf。

否則期望爲E=(a1+a2+...+acnt1)n+(b1+b2+..+bcnt2+Ecnt2)n

其中ai 是正數, bi 是負數的絕對值, cnt1,cnt2 分別是正數和負數的個數。

最後化簡結果:E=sumcnt1 , cnt1 是正數的個數,sumni=1xi

代碼:

/********************************************
 *Author*        :��ZZZZone
 *Created Time*  : 五 10/13 18:48:36 2017
 * Ended  Time*  : 五 10/13 18:54:58 2017
*********************************************/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
int T, n, Case;

int gcd(int a, int b){
    if(!b) return a;
    else return gcd(b, a % b);
}

int main()
{
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        int cnt = 0, sum = 0;
        for(int i = 1; i <= n; i++){
            int x;
            scanf("%d", &x);
            if(x > 0) cnt++;
            sum += abs(x);
        }
        printf("Case %d: ", ++Case);
        if(cnt == 0) printf("inf\n");
        else{
            int Gcd = gcd(sum, cnt);
            printf("%d/%d\n", sum / Gcd, cnt / Gcd);
        }
    }

    return 0;
}

在此輸入正文

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章