ZOJ 3827 Information Entropy

来源:互联网 发布:java 语音聊天 编辑:程序博客网 时间:2024/06/05 09:24

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3827


题意:求题目中给的式子。注意,lim 0log0 =0 (由洛必达法则)


AC代码:

#include<stdio.h>#include<math.h>#include<string.h>#define e exp(1)int main(){   int T,i,n;   double t,s,p;   char str[10];   scanf("%d",&T);   while(T--)   {      s=0;      scanf("%d%s",&n,str);      if(str[0]=='b')      t=2;      else if(str[0]=='n')      t=e;      else if(str[0]=='d')      t=10;      for(i=0;i<n;i++)      {         scanf("%lf",&p);         if(p==0)         continue;         p=p/100;         s+=p*log10(p);      }      s=-s/log10(t);      printf("%.12lf\n",s);   }   return 0;}

0 0
原创粉丝点击