Created
October 14, 2015 03:19
-
-
Save fangdingjun/c9b8c8316a4538830f43 to your computer and use it in GitHub Desktop.
Tower of Hanio problem code in golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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