//输出且计数
#include <iostream>
using namespace std;
const int discCount=3;
long move(int, char, char,char);
int main()
{
long count;
count=move(discCount,'A','B','C');
cout<<discCount<<"个盘子需要移动"<<count<<"次。"<<endl;
return 0;
}
long move(int n, char A, char B,char C)
{
long c1,c2;
if(n==1)
{
cout<<A<<"-->"<<C<<endl;
return 1;
}
else
{
c1=move(n-1,A,C,B);
cout<<A<<"-->"<<C<<endl;
c2=move(n-1,B,A,C);
return c1+c2+1;
}
汉诺塔再次加进
最新推荐文章于 2023-02-27 13:13:54 发布