From d05493906ac518baea25fbddff5ad34bbd482922 Mon Sep 17 00:00:00 2001 From: Yu Zhao Date: Tue, 8 May 2018 22:57:35 +0800 Subject: [PATCH] update --- ch01/1_35.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 ch01/1_35.go diff --git a/ch01/1_35.go b/ch01/1_35.go new file mode 100644 index 0000000..ed48293 --- /dev/null +++ b/ch01/1_35.go @@ -0,0 +1,29 @@ +// Hanoi + +package main + +import ( + "fmt" + "os" + "strconv" +) + +func main() { + num, _ := strconv.Atoi(os.Args[1]) + fmt.Println(num) + TowersOfHanoi(num) +} + +func TowersOfHanoi(num int) { + fmt.Println("The sequence of moves involved in the Tower of Hanoi are:") + TOHUtil(num, "A", "C", "B") +} + +func TOHUtil(num int, from string, to string, temp string) { + if num == 0 { + return + } + TOHUtil(num - 1, from, temp, to) + fmt.Println("Move disk ", num, " from peg ", from, " to peg ", to) + TOHUtil(num - 1, temp, to, from) +}