经典递归问题
主要是想了一会怎么先输出总共的步数count。。后来看讨论发现,步数是可以确定的 count = 2^n -1;
不过也好 用另外一种方法求得了结果
知道了sprintf 的强大了 可以把数字转换成字符串哦
#include <iostream>
#include<string>
#include<cstdio>
using namespace std;
int count;
string tem;
void hanNuo(int n,string start,string mid,string end)
{
count++;
if(n == 1)
{
//cout<<"1 from "<<start<<" to "<<end<<endl;
tem += "1 from " + start +" to " + end + "\n";
return;
}
hanNuo(n-1,start,end,mid);
// cout<<n<<" from "<<start<<" to "<<end<<endl;
char tem_n[3];
sprintf(tem_n,"%d",n);
tem += string(tem_n) + " from " + start +" to " + end + "\n";
hanNuo(n-1,mid,start,end);
}
int main()
{
int n;
cin>>n;
count = 0;
hanNuo(n,"A","B","C");
cout<<count<<endl<<tem;
return 0;
}