The Go language implements the hanotar algorithm

  • 2020-05-27 05:55:07
  • OfStack

hano.go


package main
import (
  "bufio"
  "fmt"
  "os"
  "strconv"
)
func main() {
  fmt.Print(" Enter the number of plates to move: ")
  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-->%c\n", a, c)
    hanoi(n-1, b, a, c)
  } else {
    fmt.Printf("%c-->%c\n", a, c)
  }
}

That's all for this article, I hope you enjoy it.


Related articles: