PAT 1082. Read Number in Chinese (25)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai

先将数字对应字符串的值和位对应字符串的值构造出来。如果0位置的字符是负号,就将字符串的0位置去掉。每次去输出0位置的值,如果0位置的值是零并且是万位,就输出Wan;如果0位置的值不是零,查看当前位置的前几次有没有零值,有就要输出ling,然后输出0位置的值和所在的位。

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
#include <iostream>
#include <map>
#include <string>

using namespace std;

map<char, string> initMap() {
map<char, string> res;
res['0'] = "ling";
res['1'] = "yi";
res['2'] = "er";
res['3'] = "san";
res['4'] = "si";
res['5'] = "wu";
res['6'] = "liu";
res['7'] = "qi";
res['8'] = "ba";
res['9'] = "jiu";
return res;
}

map<int, string> initSiteMap() {
map<int, string> res;
res[2] = "Shi";
res[3] = "Bai";
res[4] = "Qian";
res[5] = "Wan";
res[6] = "Shi";
res[7] = "Bai";
res[8] = "Qian";
res[9] = "Yi";
return res;
}

int main() {
string num;
cin >> num;
if (num[0] == '-') {
cout << "Fu ";
num = num.substr(1);
}
map<char, string> unit = initMap();
map<int, string> site = initSiteMap();
bool isZero = false;
cout << unit[num[0]];
if (num.length() > 1) {
cout << " " << site[num.length()];
}
num = num.substr(1);
while (num.length() > 0) {
if (num[0] == '0') {
isZero = true;
if (num.length() == 5) {
cout << " " << site[5];
}
} else {
if (isZero) {
cout << " ling";
}
isZero = false;
cout << " " << unit[num[0]];
if (num.length() > 1) {
cout << " " << site[num.length()];
}
}
num = num.substr(1);
}
cout << "Hello World." << endl;
return 0;
}