-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_B1023.cpp
More file actions
67 lines (59 loc) · 1.24 KB
/
4_B1023.cpp
File metadata and controls
67 lines (59 loc) · 1.24 KB
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
/**
* P121 组最小数 简单贪心
*/
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
int makeMinFunc()
{
// init stack
stack<int> tempStack;
stack<int> resultStack;
int amount[10];
cout << "Input the amount of each number: " << endl;
for (int i = 0; i < 10; i++)
{
cin >> amount[i];
}
// 最高位不能是 0,从 1~9 中选取最小且 amount[i] != 0
for (int j = 1; j < 10; j++)
{
if (amount[j] != 0)
{
// push
tempStack.push(j);
amount[j] -= 1;
break;
}
}
// 剩余位
for (int k = 0; k < 10; k++)
{
// 当 k 的个数不止一个
for (int l = 0; l < amount[k]; l++)
{
tempStack.push(k);
}
}
// 顺序交换
while (!tempStack.empty())
{
resultStack.push(tempStack.top());
tempStack.pop();
}
// 最后,高位在栈底,末位在栈顶
int result = 0;
while (!resultStack.empty())
{
result *= 10;
result += resultStack.top();
resultStack.pop();
}
return result;
}
int main()
{
cout << "The minimum number is: " << makeMinFunc() << endl;
return 0;
}