Skip to content

Instantly share code, notes, and snippets.

@fangdingjun
Created October 14, 2015 03:19
Show Gist options
  • Save fangdingjun/c9b8c8316a4538830f43 to your computer and use it in GitHub Desktop.
Save fangdingjun/c9b8c8316a4538830f43 to your computer and use it in GitHub Desktop.
Tower of Hanio problem code in golang
package main
import (
"fmt"
"os"
"strconv"
)
var count int
func hanoi(n int, a, b, c string){
if n == 1{
count++
fmt.Printf("%d: move sheet %d from %s to %s\n",count, n, a, c)
return
}
hanoi(n-1, a, c, b)
count++
fmt.Printf("%d: move sheet %d from %s to %s\n", count, n, a,c)
hanoi(n-1, b, a, c)
}
func main(){
var n int = 3
var err error
if len(os.Args) == 2{
n, err = strconv.Atoi(os.Args[1])
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(-1)
}
}
hanoi(n, "A", "B", "C")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment