你可以尝试用STL中的优先队列
by CiXuKun @ 2022-12-11 12:45:49
@[sixiang123bc](/user/560868) 有没有一种可能,我不会优先队列
by WangWang6 @ 2022-12-11 12:56:05
有没有一种可能,优先队列可以这么用:
```cpp
#include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
class this_is_main
{
priority_queue<int,vector<int>,greater<int>>a;
int b,c,d=0,e;
public:
this_is_main()
{
cin >> b;
for (int i = 0; i < b; i++)
{
cin >> c;
a.push(c);
}
while (a.size() > 1)
{
e = 0;
e += a.top();
a.pop();
e += a.top();
a.pop();
a.push(e);
d += e;
}
cout << d;
}
};
this_is_main run_main;
int main()
{
return 0;
}
```
(p.s. 别管我的this_is_main类,这是之前整活搞的,现在懒得改了。这就相当于将构造函数中的代码写到主函数里,将类的私有成员写成全局变量)
by GodForever @ 2023-01-01 16:11:26
![](//图.tk/a)
by MornHus @ 2023-01-06 22:34:15