站长资源脚本专栏
Go语言实现汉诺塔算法
简介hano.go package mainimport ("bufio""fmt""os""strconv")func main() {fmt.Print("输入要移动的盘子数:")reader := bufio.NewReader(os.Stdin)lool:data, _, _ := re
hano.go
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
fmt.Print("输入要移动的盘子数:")
reader := bufio.NewReader(os.Stdin)
lool:
data, _, _ := reader.ReadLine()
n, err := strconv.Atoi(string(data))
if err != nil {
fmt.Println(err)
goto lool
}
hanoi(n, 'A', 'B', 'C')
}
func hanoi(n int, a, b, c byte) {
if n > 1 {
hanoi(n-1, a, c, b)
fmt.Printf("%c-->%c\n", a, c)
hanoi(n-1, b, a, c)
} else {
fmt.Printf("%c-->%c\n", a, c)
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇:go语言遍历文件夹示例
下一篇:go语言获取系统盘符的方法