【Go 語言社群】演算法課程 第一季 第4節-漢諾塔
阿新 • • 發佈:2022-05-04
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-->%cn", a, c) hanoi(n-1, b, a, c) } else { fmt.Printf("%c-->%cn", a, c) } }