func g(str string) string {
var i = 0
var new_str = ""
for i < len(str) - 1 {
new_str = new_str + str[i + 1]
i = i + 1
}
func f(str string) string {
if len(str) == 0 {
return ""
} else {
if len(str) == 1 {
return str
} else {
return f(g(str)) + str[0]
}
}
func h(n uint64, str string) string {
for n != 1 {
if n % 2 == 0 {
n = n / 2
} else {
n = 3*n + 1
}
str = f(str)
}
return str
}
func pow(x, y uint64) uint64 {
if y == 0 {
return 1
} else {
return x * pow(x, y-1)
}
}
func main() {
fmt.Println(h(1, "fruits"))
fmt.Println(h(2, "fruits"))
fmt.Println(h(5, "fruits"))
fmt.Println(h(pow(2, 1000000000000000), "fruits"))
fmt.Println(h(pow(2, 1000000000000000), "fruits"))
}
命令行参数
。\fruits.go:11:21:无效操作:new_str+str[i+1](字符串和字节类型不匹配(.\fruits.go:24:无效操作:f(g(str((+str[0](字符串和字节类型不匹配(
错误消息描述您的问题:invalid operation: new_str + str[i + 1] (mismatched types string and byte)
。CCD_ 2是来自底层CCD_ 3阵列的一个字节。Go需要显式转换。写入string(str[i + 1])
。
例如,
package main
func g(str string) string {
var i = 0
var new_str = ""
for i < len(str)-1 {
// invalid operation: new_str + str[i + 1] (mismatched types string and byte)
// new_str = new_str + str[i+1]
new_str = new_str + string(str[i+1])
i = i + 1
}
return new_str
}
func main() {}
游乐场:https://play.golang.org/p/E9-n7IO-Q_z