标签:style http color io os ar for sp on
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);
}
}
ZOJ Problem Set - 3827Information Entropy
标签:style http color io os ar for sp on
原文地址:http://blog.csdn.net/u012997373/article/details/40074479