-
Notifications
You must be signed in to change notification settings - Fork 23
/
0042.cpp
64 lines (60 loc) · 1.49 KB
/
0042.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
// 0042.学英语
#include <iostream>
using namespace std;
string ge[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven","eight", "nine"};
string ot[10] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
string shi[10] = {"zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
string printfHundred(int n)
{
string s = "";
bool hasHundred = false;
if(n/100 != 0) {
s += ge[n/100] + " hundred";
hasHundred = true;
}
n %= 100;
if(n != 0) {
if (hasHundred) {
s += " and ";
}
if (n < 10) {
s += ge[n];
}else if (n < 20) {
s += ot[n-10];
}else {
s += shi[n/10];
if(n%10){
s += " " + ge[n%10];
}
}
}
return s;
}
int main()
{
int n;
while (cin >> n)
{
if (n == 0) {
cout << "zero" << endl;
continue;
}
if (n/1000000000 != 0) {
cout << ge[n/1000000000] << " billion ";
}
n %= 1000000000;
if(n/1000000 != 0) {
cout << printfHundred(n/1000000) << " million ";
}
n %= 1000000;
if(n/1000 != 0) {
cout << printfHundred(n/1000) << " thousand ";
}
n %= 1000;
if(n != 0) {
cout << printfHundred(n);
}
cout << endl;
}
return 0;
}