-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathA1082.cpp
More file actions
123 lines (108 loc) · 2.68 KB
/
Copy pathA1082.cpp
File metadata and controls
123 lines (108 loc) · 2.68 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
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
/*
* hint:
* 简化逻辑的充分条件是:在编写代码前充分思考
*/
#include <iostream>
#include <cstring>
#define MAXN 15
using namespace std;
const char ch_num[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"},
base[4][5] = {"", "Shi", "Bai", "Qian"};
int main()
{
bool hasOutput = false, innerNumber = false;
char tmp[MAXN];
int num[MAXN] = {};
cin.getline(tmp, MAXN);
int len = strlen(tmp), i = 0;
// special judge
if (len == 1)
{
printf("%s", ch_num[tmp[0]-'0']);
return 0;
}
else if (len == 2 && tmp[0] == '-')
{
printf("Fu %s", ch_num[tmp[1]-'0']);
return 0;
}
// get rid of the sign
if (tmp[0] == '-')
{
printf("Fu");
hasOutput = true;
i = 1;
}
// standardization
for (; i < len; i++)
num[len-i] = tmp[i] - '0';
// output 1 highest digits
if (num[9] != 0)
{
if (hasOutput)
{
printf(" %s Yi", ch_num[num[9]]);
innerNumber = true;
}
else
{
printf("%s Yi", ch_num[num[9]]);
hasOutput = true;
}
}
// output 4 middle digits
bool hasZero = false, hasOutputInThisSection = false;
for (int i = 8; i >= 5; i--)
{
if (num[i] != 0) // non-0 digit
{
// handle 0
if (hasZero)
{
printf(" ling");
hasZero = false;
}
// handle number
if (hasOutput) printf(" ");
printf("%s", ch_num[num[i]]);
// handle base
if ((i-1) % 4 != 0) printf(" %s", base[(i-1)%4]);
// handle status
hasOutput = true;
hasOutputInThisSection = true;
innerNumber = true;
}
else // 0 digit
{
if (innerNumber) hasZero = true;
}
}
if (hasOutputInThisSection) printf(" Wan");
if (hasOutput && !hasOutputInThisSection) printf(" ling");
// output 4 low digits
hasZero = false;
for (int i = 4; i >= 1; i--)
{
if (num[i] != 0) // non-0 digit
{
// handle 0
if (hasZero)
{
printf(" ling");
hasZero = false;
}
// handle number
if (hasOutput) printf(" ");
printf("%s", ch_num[num[i]]);
// handle base
if ((i-1) % 4 != 0) printf(" %s", base[(i-1)%4]);
// handle status
hasOutput = true;
}
else // 0 digit
{
if (innerNumber) hasZero = true;
}
}
return 0;
}