-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteger_to_English_Words.cpp
More file actions
62 lines (57 loc) · 1.69 KB
/
Integer_to_English_Words.cpp
File metadata and controls
62 lines (57 loc) · 1.69 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
62
//返回Num的字符串形式,num<1000
string helper(int num)
{
vector<string> less_ten=
{ "", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine" };
vector<string> less_twenty =
{ " Ten", " Eleven", " Twelve", " Thirteen", " Fourteen", " Fifteen", " Sixteen", " Seventeen", " Eighteen", " Nineteen" };
vector<string> less_hundred =
{ "", "", " Twenty", " Thirty", " Forty", " Fifty", " Sixty", " Seventy", " Eighty", " Ninety" };
string s;
if (num != 0)
{
//获取个十百位
int hundred = num / 100;
num %= 100;
int tenth = num / 10;
int single = num % 10;
if (hundred) s = s + less_ten[hundred] + " Hundred";
if (tenth)
{
if (tenth == 1)
{
//10-19之间的特殊数字
s += less_twenty[single];
return s;
}
else s += less_hundred[tenth];
}
if (single) s += less_ten[single];
}
return s;
}
//Integer to English Words,https://leetcode.com/problems/integer-to-english-words/
//字符串处理,参考:https://leetcode.com/discuss/55254/c-solution-4ms
string numberToWords(int num)
{
if (num == 0)
return "Zero";
//第一个元素起到占位的作用
vector<string> unit=
{ "", " Thousand", " Million", " Billion" };
int parts[4] = { 0 };
for (int i = 0; i < 4; ++i)
{
parts[i] = num % 1000;
num /= 1000;
}
string s;
for (int i = 0; i < 4; ++i)
{
if (parts[i] == 0) continue;
s = helper(parts[i]) + unit[i] + s;
}
//删除首部的空格符
s.erase(s.begin());
return s;
}