蓝桥杯 基础练习——Huffuman树

题目链接

 


解题报告:

蓝桥杯 基础练习——Huffuman树

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct cmp {
 4     bool operator()(int a,int b){
 5         return a>b;
 6     }
 7 };
 8 int a[110];
 9 int main()
10 {
11     int n,v;
12     priority_queue<int,vector<int>,cmp>que;
13     cin>>n;
14     for(int i=1;i<=n;i++) {
15         cin>>v;
16         que.push(v);
17     }
18     int sum=0;
19     if(n==1) {
20         cout<<v<<endl;
21     }else {
22         while(que.size()!=1) {
23             int a=que.top();
24             que.pop();
25             int b=que.top();
26             que.pop();
27             sum+=a+b;
28             que.push(a+b);
29         }
30         cout<<sum<<endl;
31     }
32     return 0;
33 }

View Code

 

原文链接: https://www.cnblogs.com/wuliking/p/12670270.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    蓝桥杯 基础练习——Huffuman树

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/340967

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午12:51
下一篇 2023年3月2日 上午12:52

相关推荐