您的位置:首页 > 运维架构

zoj 3827 Information Entropy(水题)

2014-10-13 09:29 381 查看
题目链接:zoj 3827 Information Entropy

题目大意:三种底,计算和。

解题思路:调用库函数就可以直接算了,不过要注意Pi = 0的时候,不过它题目里居然也讲了。。。limp→0+plogb(p)=0,因为p是logp的高阶。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

using namespace std;

int N;

double f(double x, int k) {
if (x == 0)
return 0;

if (k == 1)
return x * log2(x);
else if (k == 2)
return x * log(x);
else
return x * log10(x);
}

int main () {
int cas, k;
scanf("%d", &cas);
while (cas--) {
char op[10];
scanf("%d%s", &N, op);
if (op[0] == 'b')
k = 1;
else if (op[0] == 'n')
k = 2;
else if (op[0] == 'd')
k = 3;

int x;
double ans = 0;
for (int i = 1; i <= N; i++) {
scanf("%d", &x);
ans += f(x / 100.0, k);
}
printf("%.8lf\n", -ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: