-
Notifications
You must be signed in to change notification settings - Fork 0
/
1082_Read_Number_in_Chinese.cpp
74 lines (71 loc) · 1.98 KB
/
1082_Read_Number_in_Chinese.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
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
if (stoi(str) == 0) {
cout << "ling" << endl;
return 0;
}
stack<string> ans;
bool isNegative = false;
if (str[0] == '-') {
str = str.substr(1);
isNegative = true;
}
int index = str.length() - 1;
string dummy[4] = {"+", "Shi", "Bai", "Qian"};
string numInCn[10] = {"ling", "yi", "er", "san", "si",
"wu", "liu", "qi", "ba", "jiu"};
while (index >= 0) {
bool notZero = false;
for (int i = 0; i < 4 && index >= 0; ++i, --index) {
if (str[index] == '0' && !notZero)
continue;
else {
if (str[index] == '0') {
ans.push(numInCn[str[index] - '0']);
} else {
if (i != 0) ans.push(dummy[i]);
ans.push(numInCn[str[index] - '0']);
}
notZero = true;
}
}
notZero = false;
if (index >= 0) ans.push("Wan");
for (int i = 0; i < 4 && index >= 0; ++i, --index) {
if (str[index] == '0' && !notZero)
continue;
else {
if (str[index] == '0') {
ans.push(numInCn[str[index] - '0']);
} else {
if (i != 0) ans.push(dummy[i]);
ans.push(numInCn[str[index] - '0']);
}
notZero = true;
}
}
if (index >= 0) {
ans.push("Yi");
ans.push(numInCn[str[index] - '0']);
--index;
}
}
if (isNegative) {
cout << "Fu";
while (!ans.empty()) {
cout << " " << ans.top();
ans.pop();
}
} else {
cout << ans.top();
ans.pop();
while (!ans.empty()) {
cout << " " << ans.top();
ans.pop();
}
}
return 0;
}