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
| package easy
import (
"fmt"
"math"
"strings"
)
func HJ4(str string) string {
if str == "" {
return ""
}
var build strings.Builder
length := int(math.Ceil(float64(len(str)) / 8.0))
arr := make([]string, length)
// len(str) <= 8
if len(str) <= 8 {
n := 8 - len(str)
build.WriteString(str)
for n > 0 {
build.WriteString("0")
n--
}
return build.String()
}
p1 := 0
index := 0
p2 := 7
for p1 < len(str) {
build.WriteString(string(str[p1]))
if p1 == p2 {
arr[index] = build.String()
build.Reset()
index++
p2 = p1 + 8
}
p1++
}
if p1 < p2 {
c := p2 - p1
for c >= 0 {
build.WriteString("0")
c--
}
arr[index] = build.String()
}
res := ""
for _, v := range arr {
res += fmt.Sprintf("%s\n", v)
}
fmt.Println(arr)
return res
}
|