#include <iostream> using namespace std; void hnt(int n,char a,char b,char c)//用递归求最汉诺塔 { if(n==1) { cout<<a<<"->"<<c<<endl; } else { hnt(n-1,a,c,b); cout<<a<<"->"<<c<<endl; hnt(n-1,b,a,c); } } main() { int n; char a='a',b='b',c='c'; cin>>n; hnt(n,a,b,c); }
运行结果: