-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteger-to-english-words.cpp
33 lines (33 loc) · 1.56 KB
/
integer-to-english-words.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
class Solution {
public:
string word(int num) {
if (num >= 1000000000) {
return word(num / 1000000000) + " Billion" +
(num % 1000000000 ? " " : "") + word(num % 1000000000);
} else if (num >= 1000000) {
return word(num / 1000000) + " Million" +
(num % 1000000 ? " " : "") + word(num % 1000000);
} else if (num >= 1000) {
return word(num / 1000) + " Thousand" + (num % 1000 ? " " : "") +
word(num % 1000);
} else if (num >= 100) {
return word(num / 100) + " Hundred" + (num % 100 ? " " : "") +
word(num % 100);
} else if (num >= 20) {
vector<string> tens = {"", "Ten", "Twenty", "Thirty",
"Forty", "Fifty", "Sixty", "Seventy",
"Eighty", "Ninety"};
vector<string> ones = {"", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine"};
return tens[num / 10] + (num % 10 ? " " : "") + ones[num % 10];
} else {
vector<string> ones = {
"", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
return ones[num];
}
}
string numberToWords(int num) { return num ? word(num) : "Zero"; }
};