-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0098.cpp
172 lines (162 loc) · 4.1 KB
/
0098.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 0098.自动售货系统
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct {
int pri;
int num;
} goods[7] = {{0,0}, {2,0}, {3,0}, {4,0}, {5,0}, {8,0}, {6,0}},
coins[5] = {{0,0}, {1,0}, {2,0}, {5,0}, {10,0}};
struct node
{
string name;
int pri;
int num;
bool operator < (node no) const {
if (num == no.num) return name < no.name;
return num > no.num;
}
};
int pay = 0;
bool init(string cmd)
{
int i;
pay = 0;
cmd = cmd.substr(2);
string s = cmd.substr(0, cmd.find(' '));
for(i = 1, s += '-'; i <= 6; i++)
{
int idx = s.find('-');
string t = s.substr(0,idx);
s = s.substr(idx+1);
goods[i].num = stoi(t);
goods[0].num += goods[i].num;
}
s = cmd.substr(cmd.find(' ')+1);
for(i = 1, s += '-'; i <= 4; i++)
{
int idx = s.find('-');
string t = s.substr(0, idx);
s = s.substr(idx+1);
coins[i].num = stoi(t);
}
cout << "S001:Initialization is successful" << endl;
return true;
}
int check_pay(string cmd) {
int p = stoi(cmd.substr(2));
if (p != 1 && p != 2 && p != 5 && p != 10) {
cout << "E002:Denomination error" << endl;
}else if (p > coins[1].num + coins[2].pri * coins[2].num) {
cout << "E003:Change is not enough, pay fail" << endl;
}else if (p > 10) {
cout << "E004:Pay the balance is beyond the scope biggest" << endl;
}else if (goods[0].num == 0) {
cout << "E005:All the goods sold out" << endl;
} else {
for(int i = 1; i <= 4; i++) {
if(p == coins[i].pri) {
coins[i].num ++;
break;
}
}
pay += p;
cout << "S002:Pay success,balance=" << pay << endl;
}
return 0;
}
int buy(string cmd)
{
string s = "A1A2A3A4A5A6";
string name = cmd.substr(2);
int g = name[1]-'0';
if (s.find(name) == s.npos) {
cout << "E006:Goods does not exist" << endl;
} else if (goods[g].num == 0) {
cout << "E007:The goods sold out" << endl;
} else if (goods[g].pri > pay) {
cout<<"E008:Lack of balance"<<endl;
} else {
pay -= goods[g].pri;
goods[g].num --;
goods[0].num --;
cout<<"S003:Buy success,balance="<<pay<<endl;
}
return 0;
}
int return_coin()
{
int i;
int ret[5] = {0,0,0,0,0};
if (pay == 0) {
cout<<"E009:Work failure";
return -1;
}
for(i = 4; pay != 0 && i>=1; i--)
{
while(pay >= coins[i].pri && coins[i].num > 0)
{
pay-=coins[i].pri;
coins[i].num --;
ret[i] ++;
}
}
for(i = 1; i <= 4; i++)
cout << coins[i].pri<<" yuan coin number=" << ret[i] << endl;
return 0;
}
int query(string cmd)
{
int num = -1;
vector<node> v;
cmd = cmd.substr(2);
if(cmd.size()==1) num = cmd[0]-'0';
if (num == 0) {
for(int i = 1; i <= 6; i++)
{
string name = "A";
name += '0' + i;
node no = {name, goods[i].pri, goods[i].num};
v.push_back(no);
}
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++)
{
cout << v[i].name <<' '<< v[i].pri << ' ' << v[i].num << endl;
}
} else if (num == 1) {
for(int i = 1; i <= 4; i++)
cout << coins[i].pri << " yuan coin number=" << coins[i].num << endl;
}
else
cout << "E010:Parameter error";
return 0;
}
int main()
{
string cmd;
while(getline(cin,cmd))
{
pay = 0;
while(!cmd.empty())
{
int idx = cmd.find(';');
string tmp = cmd.substr(0, idx);
if(tmp[0] == 'r') {
init(tmp);
} else if(tmp[0] == 'p') {
check_pay(tmp);
} else if(tmp[0] == 'b') {
buy(tmp);
} else if(tmp[0] == 'c') {
return_coin();
} else if(tmp[0] == 'q') {
query(tmp);
}
cmd = cmd.substr(idx+1);
}
}
return 0;
}