递归---自己玩自己
递归边界+只关注下一级规模(1010不好算,101就非常好算了)
汉诺塔
#include<stdio.h>
#include<iostream>
using namespace std;
void mov(char from,char to,int ans){
cout<<"将"<<ans<<"盘从"<<from<<"杆移到"<<to<<"杆"<<endl;
}
void hanoi(char from,char to,char mid,int ans){
if(ans == 0){
return;
}
hanoi(from,mid,to,ans - 1);
mov(from,to,ans);
hanoi(mid,to,from,ans - 1);
}
int main(){
hanoi('A','C','B',3);
return 0;
}
网友评论