LightOJ 1342 Aladdin and the Magical Sticks (期望)

本文探讨了一种棍子收集问题,其中棍子分为可识别和不可识别两类,通过数学方法计算收集所有棍子的权值和的期望。文章详细解释了对于不同类型的棍子如何计算其期望权值,以及实现这一过程的C++代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:地上有n种棍子,其中有两种类型,一种类型是可识别,一种类型是不可识别,每个棍子都有一个权值。当你捡到可识别的,那么你以后就不会再捡这个棍子,如果是不可识别的,那么你有可能还会捡。每捡一根获得一个权值。问将所有棍子收集完的权值和的期望。

题解:期望
对于可识别的棍子,直接累加就可以。
对于不可识别的一根棍子,至少捡起一次期望次数=1+1/2+1/3+···+1/n(邮票收集问题),然后乘以权值就可以了。

#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;
const int maxn = 5e3 + 5;
int t, n, a, b;
double f[maxn];
int main() {
	for (int i = 1; i < maxn; i++) {
		f[i] = f[i - 1] + 1.0 / i;
	}
	scanf("%d", &t);
	int cas = 0;
	while (t--) {
		scanf("%d", &n);
		double ans = 0;
		for (int i = 1; i <= n; i++) {
			scanf("%d%d", &a, &b);
			if (b == 1) {
				ans += a;
			}
			else {
				ans += a * f[n];
			}
		}
		printf("Case %d: %f\n", ++cas, ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值