谈到递归,不得不说起汉诺塔,其实现代码如下:
//include<bits/stdc++.h>
#include <iostream>
#include<cstdio>
using namespace std;
void fun(int n,char a,char b,char c){
if(n==1){
printf("Move %d from %c to %c\n",n,a,c);
}else{
fun(n-1,a,c,b);
printf("Move %d from %c to %c\n",n,a,c);
fun(n-1,b,a,c);
}
}
int main() {
int n;
cin>>n;
fun(n,'A','B','C');
return 0;
}