题意:一个人在迷宫内,面前有n扇门,每个门都对应一个时间,如果时间是正数a,表示通过这扇门a分钟后可以离开迷宫,如果时间是负数a,表示通过这扇门abs(a)分钟后会重新回到这里,一个人任意选择n扇门,且如果回到这里会忘记之前选过的门,问离开迷宫的期望时间。
题解:可以令x是离开迷宫的期望时间,那么对应第三组样例,x = 1/3 * 3 + 1/3 * (x + 6) + 1/3 * (x + 9),解方程后就能得到x,注意结果需要化简。
#include <stdio.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = 105;
int n, a[N];
int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
int cnt = 0, sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
if (a[i] < 0)
cnt++;
sum += fabs(a[i]);
}
if (cnt == n)
printf("Case %d: inf\n", cas++);
else {
int temp = __gcd(sum, n - cnt);
printf("Case %d: %d/%d\n", cas++, sum / temp, (n - cnt) / temp);
}
}
return 0;
}