-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddAll2.cpp
82 lines (70 loc) · 1.91 KB
/
addAll2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <algorithm>
#include <fstream>
#include <functional> //为了使用优先队列的greater
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("addAll.in", "r", stdin);
freopen("addAll.out", "w", stdout);
ios::sync_with_stdio(false);
#endif
while (true)
{
/*****************************************
* 输入数据
*****************************************/
int total = 0;
cin >> total;
if (total == 0)
{
break;
}
//放入优先队列,小的数优先出来
priority_queue<int, vector<int>, greater<int>> numbersQueue;
for (int i = 0; i <= total - 1; ++i)
{
int temp;
cin >> temp;
numbersQueue.push(temp);
}
/*****************************************
* 开始加法运算
*****************************************/
int totalCost = 0;
while (true)
{
int num1 = numbersQueue.top();
numbersQueue.pop();
if (numbersQueue.empty())
{
//判断如果只有一个数就退出
break;
}
int num2 = numbersQueue.top();
numbersQueue.pop();
//两个数相加的cost
int currCost = num1 + num2;
//输出测试过程
//cout << num1 << "+" << num2 << "=" << currCost << "\n";
//累计cost
totalCost += currCost;
if (numbersQueue.empty())
{
break;
}
else
{
//使用currCost替代num1,num2
numbersQueue.push(currCost);
}
}
cout << totalCost << "\n";
}
return 0;
}