-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
41 lines (37 loc) · 1.39 KB
/
main.cpp
File metadata and controls
41 lines (37 loc) · 1.39 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
#include <iostream>
#include <vector>
#include <string>
class Solution {
public:
std::string singles[20] = {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
std::string tens[10] = {"Zero", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
std::string numberToWords(int num) {
if (num == 0) {
return "Zero";
}
std::string result = helper(num);
return result.substr(1);
}
std::string helper(int num) {
if (num >= 1000000000) {
return helper(num / 1000000000) + " Billion" + helper(num % 1000000000);
} else if (num >= 1000000) {
return helper(num / 1000000) + " Million" + helper(num % 1000000);
} else if (num >= 1000) {
return helper(num / 1000) + " Thousand" + helper(num % 1000);
} else if (num >= 100) {
return helper(num / 100) + " Hundred" + helper(num % 100);
} else if (num >= 20) {
return " " + tens[num / 10] + helper(num % 10);
} else if (num >= 1) {
return " " + singles[num];
} else {
return "";
}
}
};
int main() {
Solution solution;
std::cout << solution.numberToWords(1337) << std::endl;
return 0;
}