LightOJ 1027 A Dangerous Maze (期望)

题意:给出n扇门,每扇门都给出一个数x,若为正数,则表示在x时间后走出迷宫,若为负数,则表示在x时间后回到起点,并忘记刚刚走了哪条路,求最后的期望时间,结果以分数呈现。

题解:期望
设总的期望为 e e e
对于其中的一扇门:
若为正数:走出去的期望为 x / n x/n x/n
若为负数:走回来的期望为 x / n x/n x/n,再走出去的期望为 ( x + e ) / n (x+e)/n (x+e)/n
把所有门的情况加起来就是 e e e,然后解方程求一下。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<fstream>
#include<set>
#include<map>
#include<sstream>
#include<iomanip>
#define ll long long
using namespace std;
int t, n, a[111];
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int main() {
	scanf("%d", &t);
	int cas = 0;
	while (t--) {
		int sa = 0, sb = 0, numa = 0;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", &a[i]);
			if (a[i] > 0) sa += a[i], numa++;
			else sb += abs(a[i]);
		}
		int fenzi = sa + sb;
		int fenmu = numa;
		int m = gcd(fenzi, fenmu);
		if (fenmu) printf("Case %d: %d/%d\n", ++cas, fenzi / m, fenmu / m);
		else printf("Case %d: inf\n", ++cas);
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值