zoukankan      html  css  js  c++  java
  • 2014 牡丹江现场赛 i题 (zoj 3827 Information Entropy)

    I - Information Entropy
    Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu
    Submit Status

    Description

    Information Theory is one of the most popular courses in Marjar University. In this course, there is an important chapter about information entropy.

    Entropy is the average amount of information contained in each message received. Here, a message stands for an event, or a sample or a character drawn from a distribution or a data stream. Entropy thus characterizes our uncertainty about our source of information. The source is also characterized by the probability distribution of the samples drawn from it. The idea here is that the less likely an event is, the more information it provides when it occurs.

    Generally, "entropy" stands for "disorder" or uncertainty. The entropy we talk about here was introduced by Claude E. Shannon in his 1948 paper "A Mathematical Theory of Communication". We also call it Shannon entropy or information entropy to distinguish from other occurrences of the term, which appears in various parts of physics in different forms.

    Named after Boltzmann's H-theorem, Shannon defined the entropy Η (Greek letter Η, η) of a discrete random variable X with possible values {x1, x2, ..., xn} and probability mass function P(X) as:

    H(X)=E(ln(P(x)))

    Here E is the expected value operator. When taken from a finite sample, the entropy can explicitly be written as

    H(X)=i=1nP(xi)log b(P(xi))

    Where b is the base of the logarithm used. Common values of b are 2, Euler's number e, and 10. The unit of entropy is bit for b = 2, nat for b = e, and dit (or digit) for b = 10 respectively.

    In the case of P(xi) = 0 for some i, the value of the corresponding summand 0 logb(0) is taken to be a well-known limit:

    0log b(0)=limp0+plog b(p)

    Your task is to calculate the entropy of a finite sample with N values.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    The first line contains an integer N (1 <= N <= 100) and a string S. The string S is one of "bit", "nat" or "dit", indicating the unit of entropy.

    In the next line, there are N non-negative integers P1P2, .., PNPi means the probability of the i-th value in percentage and the sum of Piwill be 100.

    Output

    For each test case, output the entropy in the corresponding unit.

    Any solution with a relative or absolute error of at most 10-8 will be accepted.

    Sample Input

    3
    3 bit
    25 25 50
    7 nat
    1 2 4 8 16 32 37
    10 dit
    10 10 10 10 10 10 10 10 10 10
    

    Sample Output

    1.500000000000
    1.480810832465
    1.000000000000

    按照题目中所给的第二个公式求出结果,当字符为“bit”时log的底数为2当字符为“nat”时底数为e字符为“dit”时底数为10
    注意所给数据中出现0时要把0 排除
    注:求log₂X,log10X,lnx 直接调用math头文件中的log2(),log10(),log()即可
    #include <iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #define maxn 110
    using namespace std;
    int main()
    {
        int t,n;
        double a[maxn];
        double sum;
        char s[4];
        scanf("%d",&t);
        while(t--)
        {
            sum=0;
            scanf("%d %s",&n,s);
            for(int i=1;i<=n;++i)
                scanf("%lf",&a[i]);
            if(strcmp(s,"bit")==0)
            {
                for(int i=1;i<=n;++i)
                {
                    if(a[i]!=0)
                    sum-=(a[i]*0.01*((log10(a[i]*0.01)/log10(2))));
    			}
            }
            else if(strcmp(s,"nat")==0)
            {
                for(int i=1;i<=n;++i)
                {
                	if(a[i]!=0)
                    sum-=(a[i]*0.01*log(a[i]*0.01));
                }
            }
            else if(strcmp(s,"dit")==0)
            {
                for(int i=1;i<=n;++i)
                {
                	if(a[i]!=0)
                    sum-=(a[i]*0.01*log10(a[i]*0.01));
                }
            }
            printf("%.12lf
    ",sum);
        }
        return 0;
    }
    

      

  • 相关阅读:
    ionic3-ng4学习见闻--(aot方式打包问题解决方案)
    ionic3-ng4学习见闻--(轮播图完美方案)
    ionic3-ng4学习见闻--(多语言方案)
    死也要上npm 而不是cnpm苟且偷生
    2017埙箫简谱清单分享(附音频Demo)
    《好久不见》(Cover 陈奕迅)箫声远 洞箫
    React-onsenui之RouterNavigator组件解读
    基于3ds Max的游戏建模方案
    重拾SQL——从无到有
    重拾SQL——表中索值
  • 原文地址:https://www.cnblogs.com/tonghao/p/4734008.html
Copyright © 2011-2022 走看看