题意:bit为2,nat为e,dit为10,求H(X)=−∑i=1nP(xi)log b(P(xi))
题解:e取exp(1.0)即可,另外加上换底公式
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#include <stdlib.h>
#define lowbit(x) (x&-x)
//ios::sync_with_stdio(false);
typedef long long ll;
typedef long long LL;
using namespace std;
#define e exp(1.0)
int main()
{
int T;
ios::sync_with_stdio(false);
cin>>T;
int n;
string s;
while(T--)
{
double ans = 0;
cin>>n>>s;
double a;
if(s=="bit")//2
{
while(n--)
{
cin>>a;
if(a==0) continue;
a/=100.0;
ans -= (a*log(a)/log(2.0));
}
printf("%.10f\n",ans);
//cout<<ans<<endl;
}
else if(s=="nat")//e
{
while(n--)
{
cin>>a;
if(a==0) continue;
a/=100.0;
ans -= (a*log(a)/log(e));
}
printf("%.10f\n",ans);
}
else//10
{
while(n--)
{
cin>>a;
if(a==0) continue;
a/=100.0;
ans -= (a*log(a)/log(10.0));
}
printf("%.10f\n",ans);
}
}
return 0;
}