基础练习 Huffuman树


输入格式

输出格式

样例输入

样例输出

这道题到时没什么难度,慢慢想就想的出来

#include<iostream>
#include<string>
#include<sstream>
#include<stdlib.h>
using namespace std;
int main() {
int n, i, j, sum = 0;
string temp;
string arry[100];
cin >> n;
for (i = 0; i < n; ++i)
cin >> arry[i];
while (arry[1][0]) {
for (i = 0; i < n; ++i) {
temp = arry[i];
for (j = i + 1; j < n; ++j)
if (atoi(arry[j].c_str()) > atoi(temp.c_str())) {
temp = arry[j];
arry[j] = arry[i];
arry[i] = temp;
}
}
sum += (atoi(arry[n - 2].c_str()) + atoi(arry[n - 1].c_str()));
string b;
ostringstream os;
os << (atoi(arry[n - 2].c_str()) + atoi(arry[n - 1].c_str()));
arry[n - 2] = os.str();
arry[n - 1].erase();
n --;
}
cout << sum << endl;
system("pause");
}
05-11 20:19