•输入一个字符串,请按长度为8拆分每个输入字符串并进行输出;
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。
package main import ( "fmt" ) func main() { a := "" n, _ := fmt.Scan(&a) sep := 8 if n != 0 { remainder := len([]rune(a)) % sep if remainder == 0 { // } else { for sep - remainder > 0 { remainder++ a += "0" } } for i := 0; i < len([]rune(a)); i+=sep { fmt.Println(a[i:i+sep]) } } }
package main import ( "fmt" "strings" ) func main() { var a string fmt.Scan(&a) q := len(a) / 8 r := len(a) % 8 for i := 0; i < q; i++ { fmt.Println(a[i*8 : i*8+8]) } if r != 0 { fmt.Println(a[q*8:] + strings.Repeat("0", 8-r)) } }
package main import ( "bufio" "fmt" "os" ) func main() { str := "" scanner := bufio.NewScanner(os.Stdin) scanner.Scan() str = scanner.Text() if str == "" { fmt.Println() } else { if len(str) <= 8 { res := str for i := 0; i < 8-len(str); i++ { res += "0" } fmt.Println(res) } else { val := len(str) / 8 if len(str)%8 != 0 { fmt.Println(str[0:8]) for i := 1; i < val; i++ { s := str[8*i : 8*(i+1)] fmt.Println(s) } last := str[8*val:] last1 := last for i := 0; i < 8-len(last1); i++ { last += "0" } fmt.Println(last) }else{ fmt.Println(str[0:8]) for i := 1; i < val; i++ { s := str[8*i : 8*(i+1)] fmt.Println(s) } } } } }
package main import "fmt" func main() { var s string fmt.Scanln(&s) n := len(s) strs := []string{} for n >= 8 { strs = append(strs, s[:8]) n -= 8 s = s[8:] } if n > 0 { for i := 0; i < 8-n; i++ { s += "0" } strs = append(strs, s) } for i := 0; i < len(strs); i++ { fmt.Println(strs[i]) } }
package main import ( "bufio" "fmt" "os" ) func main() { scanner := bufio.NewScanner(os.Stdin) result := make([]string, 0) for scanner.Scan() { input := scanner.Text() input += "0000000" for len(input) >= 8 { result = append(result, input[0:8]) input = input[8:] } } for _, item := range result { fmt.Println(item) } }
package main import ( "bufio" "os" "fmt" ) func main(){ scanner:=bufio.NewScanner(os.Stdin) for scanner.Scan(){ input:=scanner.Text() count:=len(input) for i:=0;i<count;{ if i+8<count{ fmt.Println(input[i:i+8]) i=i+8 }else{ str:=input[i:] zeroCount:=i+8-count for j:=0;j<zeroCount;j++{ str=str+"0" } fmt.Println(str) break } } } }
package main import ( "os" "fmt" "bufio" "strings" ) func main() { scanner := bufio.NewScanner(os.Stdin) for { scanner.Scan() str := scanner.Text() if str == "" { // loop exit break } slice := strings.Split(str, "") length := len(slice) begin := 0 for i := 1; ; i++{ end := i * 8 if length >= end { fmt.Println(strings.Join(slice[begin:end], "")) if length == end { break } } else { var target string prefix := strings.Join(slice[begin:length], "") target = prefix for zeroCount := (i*8) - length; zeroCount > 0; zeroCount--{ target += "0" } fmt.Println(target) break } begin = end } } }做得比较笨。
package main import ( "fmt" ) func main () { var ( strs string err error ) _, err = fmt.Scan(&strs) for ;err==nil; { for { li := make([]byte, 0) for i:=0; i<8; i++ { if i<len(strs) { li = append(li, strs[i]) } else { li = append(li, '0') } } fmt.Println(string(li)) if len(strs)>8 { strs = strs[8:] } else { break } } _, err = fmt.Scan(&strs) } }