标签:
#include<iostream> #include<queue> #include<string> using namespace std; struct node { int lchild; int rchild; int freq; string str; friend bool operator<(node n1,node n2){ return n1.freq>n2.freq; //从小到大排序 } }a[10]; int main(){ priority_queue<node> q; int n; while(cin>>n){ for(int i=1;i<=n;i++){ cin>>a[i].freq; a[i].lchild=NULL; a[i].rchild=NULL; q.push(a[i]); } for(int i=1;i<n;i++){ node z; z.lchild=q.top().freq; q.pop(); z.rchild=q.top().freq; q.pop(); z.freq=z.lchild+z.rchild; q.push(z); } while(!q.empty()){ cout<<q.top().freq<<endl; q.pop(); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/wintersong/p/4712792.html