-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCCI1608-EnglishInt.go
More file actions
87 lines (78 loc) · 3.17 KB
/
LCCI1608-EnglishInt.go
File metadata and controls
87 lines (78 loc) · 3.17 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
// 面试题 16.08. English Int LCCI
// Given any integer, print an English phrase that describes the integer
// (e.g., "One Thousand Two Hundred Thirty Four").
// Example 1:
// Input: 123
// Output: "One Hundred Twenty Three"
// Example 2:
// Input: 12345
// Output: "Twelve Thousand Three Hundred Forty Five"
// Example 3:
// Input: 1234567
// Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
// Example 4:
// Input: 1234567891
// Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
import "fmt"
import "strings"
func numberToWords(num int) string {
if num == 0 { return "Zero" }
var (
singles = []string{"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}
teens = []string{"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}
tens = []string{"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}
thousands = []string{"", "Thousand", "Million", "Billion"}
)
sb := &strings.Builder{}
var recursion func(int)
recursion = func(num int) {
switch {
case num == 0:
case num < 10: // 1 - 9
sb.WriteString(singles[num])
sb.WriteByte(' ')
case num < 20: // 10 - 19
sb.WriteString(teens[num - 10])
sb.WriteByte(' ')
case num < 100: // 20 - 90
sb.WriteString(tens[num / 10])
sb.WriteByte(' ')
recursion(num % 10)
default:
sb.WriteString(singles[num / 100])
sb.WriteString(" Hundred ")
recursion(num % 100)
}
}
for i, unit := 3, int(1e9); i >= 0; i-- {
if cur:= num / unit; cur > 0 {
num -= cur * unit
recursion(cur)
sb.WriteString(thousands[i]) // 逢 3
sb.WriteByte(' ')
}
unit /= 1000
}
return strings.TrimSpace(sb.String())
}
func main() {
// Example 1:
// Input: 123
// Output: "One Hundred Twenty Three"
fmt.Println(numberToWords(123)) // "One Hundred Twenty Three"
// Example 2:
// Input: 12345
// Output: "Twelve Thousand Three Hundred Forty Five"
fmt.Println(numberToWords(12345)) // "Twelve Thousand Three Hundred Forty Five"
// Example 3:
// Input: 1234567
// Output: "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
fmt.Println(numberToWords(1234567)) // "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
// Example 4:
// Input: 1234567891
// Output: "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
fmt.Println(numberToWords(1234567891)) // "One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thousand Eight Hundred Ninety One"
fmt.Println(numberToWords(123456789)) // One Hundred Twenty Three Million Four Hundred Fifty Six Thousand Seven Hundred Eighty Nine
fmt.Println(numberToWords(987654321)) // Nine Hundred Eighty Seven Million Six Hundred Fifty Four Thousand Three Hundred Twenty One
}