#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; }
时间: 2024-10-12 16:22:20