递归(汉诺塔) Go实现
2013-01-16 11:08 Danny.tian 阅读(331) 评论(0) 编辑 收藏 举报Go实现:
package main import "fmt" func main() { Hanoi(3, 'A', 'B', 'C') } func Hanoi(n int, x, y, z rune) { if n == 0 { //nothing to do } else { Hanoi(n-1, x, z, y) fmt.Printf("%c->%c,", x, y) Hanoi(n-1, z, y, x) } }
C实现:
#include <stdio.h> #include <stdlib.h> void hanoi(int n, char x, char y, char z); void hanoi(int n, char x, char y, char z) { if(n == 0){ //nothing to do } else { hanoi(n - 1, x, z, y); printf("%c->%c, ", x, y); hanoi(n - 1, z, y, x); } }