ACM 2014 牡丹江区域赛I-Information Entropy (模拟)

xiaoxiao2025-11-26  7

ZOJ - 3827

题目大意:

        因为学过信息论所以就直接没细读题,就是给你n个数的概率算出他们的平均信息熵

题解:

        公式都已经给出了,并且题干还给出了p(xi)=0的情况——训练的时候我们本想求出p(xi)=0的时候limplog的极限值,结果求了个很奇怪的数,高数有些忘了www

       结果后来队友直接pi=0的时候跳过,就A掉了这道题......

       

      题目要求的精度是八位数,最好的方法就是按照题目要求和样例来看,向我们,多余保留了10位还WA了一发......

#include<bits/stdc++.h> #include<cmath> using namespace std; double p[110]; int main() { int T; cin>>T; while(T--) { int n; string s; cin>>n; cin>>s; int x; for(int i=1;i<=n;++i) { cin>>x; p[i]=x/100.0; } double b; if(s=="bit") b=2; else if(s=="nat") b=exp(1); else b=10; double ans=0.0; for(int i=1;i<=n;++i) if(p[i]!=0.0) ans+=p[i]*(log(p[i])/log(b)); ans=-ans; cout<<fixed<<setprecision(8)<<ans<<endl; } return 0; }

 

转载请注明原文地址: https://www.6miu.com/read-5040060.html

最新回复(0)