R递归实现汉诺塔——数据分析师不懂黑科技

HNT <- function(n,from='T1',to='T3',by="T2"){
if(n==1) cat("move",n,"from",from,"to",to,"\n")
if(n>1){
HNT(n-1,from,by,to)
cat("move",n,"from",from,"to",to,"\n")
HNT(n-1,by,to,from)
}
}

HNT(4)

你可能感兴趣的:(R递归实现汉诺塔——数据分析师不懂黑科技)