Gym 100187B-A Lot of Joy

题意:给一个字符串,将每个字符分开放进两个口袋,每次从两个口袋分别拿出一个字符,如果相同则开心,问开心的次数期望是多少。

分析:数学期望题,然而这是我最不拿手的。。。最后答案是每个字符在字符串出现的次数/字符串长度的和。

 

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <ctime>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <set>
 8 #include <vector>
 9 #include <sstream>
10 #include <queue>
11 #include <typeinfo>
12 #include <fstream>
13 #include <map>
14 #include <stack>
15 using namespace std;
16 const int maxn=200010;
17 char s[maxn];
18 int h[maxn];
19 int main()
20 {
21     scanf("%s",s);
22     int len=strlen(s);
23     memset(h,0,sizeof(h));
24     for(int i=0;i<len;i++){
25         h[s[i]]+=1;
26     }
27     double ans=0;
28     for(int i=0;i<len;i++){
29         ans+=h[s[i]]*1.0/(len*1.0);
30     }
31     printf("%.9f\n",ans);
32     return 0;
33 }

 

转载于:https://www.cnblogs.com/RRirring/p/4665998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值