【概率论】优惠劵收集者问题

一,概念理解

期望E=事件*事件发生的概率

二,例题 

例题1:优惠券 Coupons

地址:优惠券 Coupons - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

直接套用公式:

 只不过这道题的输出有点麻烦。

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll gcd(ll a, ll b)
{
	return b == 0 ? a : gcd(b, a % b);
}
int fenmu=1,fenzi=0;
void harmonic(int m) {
    //double sum = 0.0;
    for (int i = 1; i <= m; ++i) {
        //sum += 1.0 / i;
        fenzi=fenzi*i+1*fenmu;
        fenmu=fenmu*i;
       int x= gcd(fenzi,fenmu);
        fenzi=fenzi/x;
        fenmu=fenmu/x;
    }
}

int main() {
    int n;
    while(cin>>n)
 {   // 计算 n 的调和和
 fenmu=1,fenzi=0;
    harmonic(n);
 
    // 期望值 E
   fenzi=n*fenzi;
   int y=gcd(fenzi,fenmu);
   fenzi=fenzi/y,fenmu=fenmu/y;
   if(fenmu==1)
   {
   	cout<<fenzi<<endl;
   }
   else
   {
   	int zhen=fenzi/fenmu;
   	int cnt=0,tmp=zhen;
   	while(tmp)
   	{
   		tmp=tmp/10;
   		cnt++;
	}
	int cnt1=0,cnt2=0,tmp1=fenzi-zhen*fenmu,tmp2=fenmu;
	while(tmp1)
   	{
   		tmp1=tmp1/10;
   		cnt1++;
	}
	while(tmp2)
   	{
   		tmp2=tmp2/10;
   		cnt2++;
	}
	int res=max(cnt1,cnt2);
	for(int i=1;i<=cnt+1;i++)
	cout<<" ";
	cout<<fenzi-zhen*fenmu<<endl;
	cout<<zhen<<" ";
	
	for(int i=1;i<=res;i++)
	cout<<"-";
	cout<<endl;
		for(int i=1;i<=cnt+1;i++)
	cout<<" ";
	cout<<fenmu<<endl;
   }

}
     
    

    return 0;
}

 例题2. Club

地址:Problem - I - Codeforces

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值