ZOJ-3827-Information Entropy
来源:互联网 发布:何洁的长相 知乎 编辑:程序博客网 时间:2024/06/07 11:50
ZOJ-3827-Information Entropy
Time Limit: 2 Seconds Memory Limit: 65536 KB
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)=limp→0+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 P1, P2, .., PN. Pi means the probability of the i-th value in percentage and the sum of Pi will 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
题目链接:ZOJ 3827
以下是代码:
#include <iostream>#include <cstdio>#include <cmath>#include <vector>#include <cstring>#include <algorithm>#include <string>#include <set>#include <stack>#include <queue>#include <map>using namespace std;int main(){ int t; cin >> t; while(t--) { int n; string s; double p[200] = {0}; cin >> n >> s; double b = 0; if (s == "bit") b = 2; else if (s == "nat") b = 3; else b = 10; for (int i = 0; i < n; i++) { cin >> p[i]; p[i] /= 100; } double ans = 0; for (int i = 0; i < n;i ++) { if (p[i] == 0) ans += 0; else { double ansmid = 0; if (b == 3) ansmid = log(p[i] * 1.0); else { ansmid = 1.0 * log(p[i] * 1.0 ) / log (b * 1.0); } ans += ansmid * 1.0 * p[i]; } } ans *= -1; printf("%.12f\n",ans); } return 0;}
- ZOJ 3827 Information Entropy
- ZOJ-3827-Information Entropy
- ZOJ 3827 Information Entropy
- ZOJ-3827-Information Entropy
- zoj 3827 Information Entropy(水题)
- ZOJ 3827 Information Entropy 水
- zoj 3827 Information Entropy 【水题】
- ZOJ ——3827 Information Entropy
- ZOJ Problem Set - 3827Information Entropy
- 解题报告 之 ZOJ 3827 Information Entropy
- ZOJ 3827 Information Entropy【水题、简单相加】
- ZOJ 3827 Information Entropy (2014牡丹江区域赛)
- ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)
- zoj 3827 Information Entropy(2014牡丹江区域赛I题)
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
- 信息熵 information Entropy
- Calculate Information Entropy
- 信息熵(Information Entropy)
- ARM设置模式为管理模式
- 【时间管理】群星总比月亮闪烁
- [LeetCode(Q69)] Sqrt(x) (编程实现sqrt)
- HDU 2103 Family planning(水~)
- 强连通之HDU4635 Strongly connected
- ZOJ-3827-Information Entropy
- Java并发编程(一)线程定义、状态和属性
- HDU 2107 Founding of HDU(水~)
- Longest Consecutive Sequence
- HashMyFiles生成文件的SHA和MD5
- 关于Core Graphics中基本仿射变换
- 在Android library中不能使用switch-case语句访问资源ID的原因分析及解决方案
- ZOJ-3829-Known Notation
- 关于CCPlatform Macro.h中的宏定义