如何改正

qq_46091485 2021-01-06 11:48:47
如何将下面代码改为这个要求.输入一个整数N,表示共有N组测试数据; 接下来输入这N组的测试数据,每组测试数据由若干的整数构成,表示权值,这些整数在同-行上,以0作为每组测试数据的结束标志。 第- -行是一个整数N,表示-共有N组测试数据; #include <iostream> #include <queue> using namespace std; int main(){ //优先队列的排列顺序为小的在队头 priority_queue<int, vector<int>, greater<int>> q; int n, x; cin >> n; for(int i = 0; i < n; i++){ cin >> x; q.push(x); } int ans = 0; while(q.size()>1) { int a, b; a = q.top(); q.pop(); b = q.top(); q.pop(); q.push(a+b); ans += a+b; } cout << ans <<endl; return 0; }
...全文
89 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_46091485 2021-01-07
  • 打赏
  • 举报
回复
引用 2 楼 赵4老师的回复:
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
int main() { //优先队列的排列顺序为小的在队头
    int N;
    cin >> N;
    for (int i=0;i<N;i++) {
        priority_queue<int, vector<int>, greater<int> > q;
        int x;
        while (1) {
            cin >> x;
            if (x==0) break;
            q.push(x);
        }
        int ans = 0;
        while(q.size()>1) {
            int a, b;
            a = q.top();
            q.pop();
            b = q.top();
            q.pop();
            q.push(a+b);
            ans += a+b;
        }
        cout << ans <<endl;
    }
    return 0;
}
老师为啥子输出是这种情况 2 5 2 9 11 8 3 7 0 120 2 1 4 5 7 3 4 9 0 第三行直接输出结果了 而不是在第二组数据之后输出
赵4老师 2021-01-07
  • 打赏
  • 举报
回复
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
int main() { //优先队列的排列顺序为小的在队头
    int N;
    cin >> N;
    for (int i=0;i<N;i++) {
        priority_queue<int, vector<int>, greater<int> > q;
        int x;
        while (1) {
            cin >> x;
            if (x==0) break;
            q.push(x);
        }
        int ans = 0;
        while(q.size()>1) {
            int a, b;
            a = q.top();
            q.pop();
            b = q.top();
            q.pop();
            q.push(a+b);
            ans += a+b;
        }
        cout << ans <<endl;
    }
    return 0;
}
赵4老师 2021-01-07
  • 打赏
  • 举报
回复
#include <iostream>
#include <queue>
using namespace std;
int main() { //优先队列的排列顺序为小的在队头
    int N;
    cin >> N;
    for (int i=0;i<N;i++) {
        priority_queue<int, vector<int>, greater<int> > q;
        int x;
        while (1) {
            cin >> x;
            if (x==0) break;
            q.push(x);
        }
        int ans = 0;
        while(q.size()>1) {
            int a, b;
            a = q.top();
            q.pop();
            b = q.top();
            q.pop();
            q.push(a+b);
            ans += a+b;
        }
        cout << ans <<endl;
    }
    return 0;
}
赵4老师 2021-01-07
  • 打赏
  • 举报
回复
#include <functional>
#include <iostream>
#include <strstream>
#include <queue>
#include <string>
using namespace std;
int main() { //优先队列的排列顺序为小的在队头
    int N;
    cin >> N;
    ostrstream so;
    for (int i=0;i<N;i++) {
        priority_queue<int, vector<int>, greater<int> > q;
        int x;
        while (1) {
            cin >> x;
            if (x==0) break;
            q.push(x);
        }
        int ans = 0;
        while(q.size()>1) {
            int a, b;
            a = q.top();
            q.pop();
            b = q.top();
            q.pop();
            q.push(a+b);
            ans += a+b;
        }
        so << ans <<endl;
    }
    cout << so.str();
    return 0;
}
//输入:
//2
//5 2 9 11 8 3 7 0
//2 1 4  5 7 3 4 9 0
//输出:
//120
//98
//

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧