本文共 878 字,大约阅读时间需要 2 分钟。
题意:bit为2,nat为e,dit为10,求H(X)=−∑i=1nP(xi)log b(P(xi))
题解:e取exp(1.0)即可,另外加上换底公式
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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< < >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;}
转载地址:http://jlali.baihongyu.com/