package main
import (
"fmt"
)
func main() {
// 汉诺塔
test1(3, "A", "B", "C")
}
func test1(n int, a, b, c string) {
if n == 1 {
fmt.Printf("将%s柱子移动到%s柱子上\n", a, c)
} else {
// 将n-1(当前在A上) 借助于c移动到b上
test1(n-1, a, c, b)
fmt.Printf("将%s柱子移动到%s柱子上\n", a, c)
// 将n-1(当前在B上) 借助于A移动到C上
test1(n-1, b, a, c)
}
}
结果:
将A柱子移动到C柱子上
将A柱子移动到B柱子上
将C柱子移动到B柱子上
将A柱子移动到C柱子上
将B柱子移动到A柱子上
将B柱子移动到C柱子上
将A柱子移动到C柱子上