zoukankan      html  css  js  c++  java
  • UVa 10954,Add All

    Huffman编码简化版,优先队列搞定。

    1A

    调试的时候发现一个问题。。木有想明白。。。问题代码里给出,哪位大神给解释下。

    #include <iostream>
    #include <queue>
    #define maxn 5000+5
    using namespace std;
    int n;
    int ans;
    priority_queue<int, vector<int>, greater<int> >qi;
    int main()
    {
        while(cin>>n&&n){
            int x;
            ans=0;
            while(n--){
                cin>>x;
                qi.push(x);
            }
        int a,b;
        while (1){
            a=qi.top();
            qi.pop();
            if (qi.empty()) break;
            b=qi.top();
            qi.pop();
            ans+=a+b;
            qi.push(a+b);
        }
        cout<<ans<<endl;
        while (!qi.empty()) qi.pop();//实际上这一句就执行一次,因为就剩一树根才break
        //出来的,但是换成qi.pop()后,下次循环读不进来第一个数(经测试实际上是略过了第一个数)
        // 用的c-free,编译器mingw5,不晓得是不是编译器或是软件的问题。 
        }
    }
    View Code
  • 相关阅读:
    xlrd模块
    魔法路由ViewSetMixin
    AES加密
    orm的增删改查
    引入方式+样式+选择器
    视图+sql注入+事务+存储过程
    mysql用户管理+pymysql模块
    记录的详细操作
    约束
    一次http请求参数问题
  • 原文地址:https://www.cnblogs.com/acbingo/p/4038449.html
Copyright © 2011-2022 走看看