ZOJ 3827 Information Entropy

題目鏈接: 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;
}

發佈了41 篇原創文章 · 獲贊 17 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章