本文主要是介绍uva10954 Add All,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*
uva10954 Add All
AC by Warteac
2013-4-11
Runtime:0.048s
*/
/*
priority_queue 的定义和cmp重写
*/
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
class AddAll{
private:
struct cmp{
bool operator()(const int a,const int b){
return a > b;
}
};
priority_queue <int,vector<int>,cmp> figure;
int minTotal;
public:
void initial();
void readCase(int);
void computing();
void outResult();
};
void AddAll::initial(){
while(!figure.empty()){//clear
figure.pop();
}
minTotal = 0;
}
void AddAll::readCase(int n){
int m;
while(n--){
cin >> m;
figure.push(m);
}
}
void AddAll::computing(){
int a;
while(figure.size() > 1){
a = figure.top();
figure.pop();
a += figure.top();
figure.pop();
//cout<<"a = "<<a<<endl;
//cout<<"b = "<<b<<endl;
figure.push(a);
minTotal += a;
//cout<<"a+b = "<<a+b<<endl;
}
}
void AddAll::outResult(){
cout<<minTotal<<endl;
}
int main(){
AddAll aa;
int n;
while(cin >> n && n){
aa.initial();
aa.readCase(n);
aa.computing();
aa.outResult();
}
}
这篇关于uva10954 Add All的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!