题目链接:ZOJ 3827 Information Entropy
依据题目的公式算吧,那个极限是0
AC代码:
#include <stdio.h> #include <string.h> #include <math.h> const double e=exp(1.0); double find(char op[]) { if(op[0]=='b') return 2.0; else if(op[0]=='n') return e; else if(op[0]=='d') return 10.0; } int main() { int t,i,n; double p[210],b; char op[20]; scanf("%d",&t); while(t--) { scanf("%d %s",&n,op); b=find(op); double ans=0.0; for(i=0;i<n;i++) { scanf("%lf",&p[i]); p[i]/=100.0; if(p[i]!=0.0) ans+=p[i]*log(p[i])/log(b); } printf("%.12lf ",-ans); } return 0; }