LightOJ - 1027 (期望基礎)
題目鏈接:LightOJ - 1027
題目大意: 給你n個門,每次進每個門的概率都是一樣的,正數代表你x分鐘後可以離開這裏,負數代表你x分鐘後回到這裏。求預期離開的時間, 如果時間無限大,輸出inf。
數據範圍:
解題思路:
用
- 選了正數, 概率爲
1n , 期望爲1k∗ni , 結束。 - 選了負數, 概率爲
1n ,xi 分鐘之後回到原點, 那麼期望爲1n∗(E+xi)
如果正數個數爲0, 答案爲inf。
否則期望爲
其中
最後化簡結果:
代碼:
/********************************************
*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;
}
在此輸入正文