ZOJ Problem Set - 3827Information Entropy

来源:互联网 发布:网络助手 编辑:程序博客网 时间:2024/06/05 06:10

ZOJ Problem Set - 3827Information Entropy

题目链接

题目大意:给你一个公式,然后给你n个变量x,求出这些x代入公式所得的值之和。

解题思路:普通的利用数学函数求和,只是要知道x = 0的时候,结果等于0.

代码:

#include <cstdio>#include <cstring>#include <cmath>const double esp = 1e-9;int main () {    int T, n;    double b, p;    char str[10];    scanf ("%d", &T);    while (T--) {        scanf ("%d%s", &n, str);                if (str[0] == 'b')            b = 2;        else if (str[0] == 'n')            b = exp(1.0);        else            b = 10;        double ans = 0;        for (int i = 0; i < n; i++) {            scanf ("%lf", &p);            if (fabs(p) > esp) {                 p /= 100.0;                ans += p * log(p) / log(b);                }        }        printf ("%.12lf\n", -ans);    }}
0 0