-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCCI0502-BinaryNumberToString.go
More file actions
72 lines (63 loc) · 1.71 KB
/
LCCI0502-BinaryNumberToString.go
File metadata and controls
72 lines (63 loc) · 1.71 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
package main
// 面试题 05.02. Binary Number to String LCCI
// Given a real number between 0 and 1 (e.g., 0.72) that is passed in as a double, print the binary representation.
// If the number cannot be represented accurately in binary with at most 32 characters, print "ERROR".
// Example1:
// Input: 0.625
// Output: "0.101"
// Example2:
// Input: 0.1
// Output: "ERROR"
// Note: 0.1 cannot be represented accurately in binary.
// Note:
// This two charaters "0." should be counted into 32 characters.
// The number of decimal places for num is at most 6 digits
import "fmt"
import "strings"
func printBin(num float64) string {
var sb strings.Builder
sb.WriteString("0.")
var r float64
for num > 0 && num < 1 {
r = num * 2
if r >= 1 {
sb.WriteString("1")
num = r - 1
} else {
sb.WriteString("0")
num = r
}
if sb.Len() > 32 {
return "ERROR"
}
}
return sb.String()
}
func printBin1(num float64) string {
res, r := []byte{'0', '.'}, float64(0)
for num > 0 && num < 1 {
r = num * 2
if r >= 1 {
res, num = append(res, '1'), r - 1
} else {
res, num = append(res, '0'), r
}
if len(res) > 32 {
return "ERROR"
}
}
return string(res)
}
func main() {
// Example1:
// Input: 0.625
// Output: "0.101"
fmt.Println(printBin(0.625)) // "0.101"
// Example2:
// Input: 0.1
// Output: "ERROR"
// Note: 0.1 cannot be represented accurately in binary.
fmt.Println(printBin(0.1)) // "ERROR"
fmt.Println(printBin1(0.625)) // "0.101"
fmt.Println(printBin1(0.1)) // "ERROR"
}