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