-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger to String
More file actions
61 lines (58 loc) · 1.63 KB
/
Copy pathInteger to String
File metadata and controls
61 lines (58 loc) · 1.63 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
class Solution {
public:
#define space(ans) ((ans=="")?"":" ")
string Negative = "Negative", Hundred = "Hundred";
vector<vector<string>> place{{"Zero", "One", "Two", "Three", "Four","Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"},
{"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"},
{"", "Thousand", "Million", "Billion"}
};
string convHund(int num)
{
string ans;
int hundrds = num/100;
num = num%100;
if(hundrds)
{
ans = place[0][hundrds] + " " + Hundred;
}
if(num >= 10 and num <= 19)
{
ans += space(ans) + place[0][num];
}
else
{
if(num/10)
ans += space(ans) + place[1][num/10];
if(num%10)
ans += space(ans) + place[0][num%10];
}
return ans;
}
string convert(int num)
{
string ans ="";
int part = 0;
while(num)
{
int firstPrt = num%1000;
num = num/1000;
if(firstPrt != 0)
{
ans = convHund(firstPrt) + space(place[2][part]) + place[2][part] + space(ans) + ans;
}
part++;
}
return ans;
}
string numberToWords(int num) {
string ans = "";
if(num == 0) return place[0][0];
if(num < 0)
{
ans = Negative;
num = -num;
}
ans += convert(num);
return ans;
}
};