Swift解决汉诺塔问题(递归)
import Cocoa
print("Hanoi Problem")
func hanoi( _ a:String, _ b:String, _ c:String, _ n:Int){
// a,b,c mean three pillar and n means the num of disc left
if n==1{
print(a+"-->"+b) //move directly
}
else{
hanoi(a,b,c,n-1) //move a to c
print(a+"-->"+c)
hanoi(b,a,c,n-1) //move b to c
}
}
//Example:
var num:Int = 3 //number of disces
print("move path:")
hanoi("A","B","C",num) //run function hanoi