![](https://i-blog.csdnimg.cn/blog_migrate/8d5fe7761712be2ad7113f799e713910.png)
目录
题目描述
对于一个长度为 nn 的 01 串 S=x1x2x3…xnS=x1x2x3…xn,香农信息熵的定义为 H(S)=H(S)= −Σ1np(xi)log2(p(xi))−Σ1np(xi)log2(p(xi)),其中 p(0),p(1)p(0),p(1) 表示在这个 01 串中 0 和 1 出现的占比。
比如,对于 S=100S=100 来说,信息熵 H(S)=−13log2(13)−23log2(23)−23log2(23)=1.3083H(S)=−31log2(31)−32log2(32)−32log2(32)=1.3083。
对于一个长度为 2333333323333333 的 01 串, 如果其信息熵为 11625907.579811625907.5798, 且 0 出现次数比 1 少, 那么这个 01 串中 0 出现了多少次?
题目解答
#include <stdio.h>
#include <math.h>
int main()
{
double n = 23333333;
double sum;
int j;
for(j = n/2; j >=0; j--)
{
sum = 0;
sum -= j*(j / n)*log2(j / n);
sum -= (n-j)*((n - j) / n)*log2((n - j) / n);
if (sum > 11625907.5 && sum< 11625907.6)
break;
}
printf("%d",j);
return 0;
}