# HC_final

user_3763047219
c_cpp
2 years ago
1.4 kB
1
Indexable
Never
```#include <iostream>
#include <deque>
#include <algorithm>
#include <cstdlib>
#include <map>

using namespace std;
struct Node { //沒有*
int value;
Node* left;
Node* right;
};

void HC(Node* node,map<int, string>&ans,string anscode){  //&
if(node->left==NULL && node->right==NULL){
ans[node->value]=anscode;  //ans
}
else{
HC(node->left,ans,anscode+"0");
HC(node->right,ans,anscode+"1");
}
}

bool sortway(Node* a,Node* b){
return a->value < b->value;   //比較兩點之value比較兩點之value 小於
}

int main(){
int n=0;
cin >> n;
int f[n]={0};  //這裡沒有結構
deque<Node*> tree;  //<Node*>
Node* newnode;
for(int i=1;i<=n;i++){
cin >> f[i];
newnode= new Node;  //沒有*
newnode->value=f[i];
newnode->left=NULL;
newnode->right=NULL;
tree.push_back(newnode);  //記得放點
}

for(int i=1;i<=n-1;i++){
sort(tree.begin(),tree.end(),sortway);
newnode = new Node;
newnode->value = tree[0]->value + tree[1]->value;
newnode->left = tree[0];
newnode->right = tree[1];
tree.pop_front();
tree.pop_front();
tree.push_back(newnode);  //記得把點放回去
}
Node* root =tree.front();

map<int, string>ans;
HC(root,ans,"");

cout << n;
cout << "\n";
for(int i=1;i<n;i++){
cout << ans[f[i]] << " ";
}
cout << ans[f[n]];
}```