Huffman哈弗曼编码长度
直接排序然后做即可
代码如下
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <numeric>
#include <cmath>
#include <regex>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
//哈弗曼编码
void Huffman();
int main()
{
Huffman();
system("pause");
}
//很简单,自己理解吧
void Huffman()
{
const int SIZE = 6;
double a[SIZE] = { 0.1,0.2,0.3,0.4,0.5,0.5 };
vector<double> res;
for (int i = 0; i<SIZE; i++)
res.push_back(a[i]);
sort(res.begin(), res.end());
double sum = 0.0;
while (res.size() >= 2)
{
double a = res[0] + res[1];
cout << res[0] << " " << res[1] << endl;
sum += a;
res.push_back(a);
res.erase(res.begin());
res.erase(res.begin());
sort(res.begin(), res.end());
}
cout << sum << endl;
}