如何將下面代碼改為這個要求.輸入一個整數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; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/245443.html
標籤:C++ 語言
上一篇:C語言
下一篇:c語言程式設計
