如下面汉诺塔的递归,要怎样才能得出A、B、C的移动次数?
递归中可以用 int i; i++; cout<<i; 计算次数吗?或可以用什么办法?
#include<iostream>
using namespace std;
void move(int n,char source,char destination,char spare);
int main()
{
const char PEG1='A',
PEG2='B',
PEG3='C';
cout<<"This program solves the Hanoi Towers puzzle.\n\n";
cout<<"Enter the nuimber of disks: ";
int numDisks;
cin>>numDisks;
cout<<endl;
move(numDisks,PEG1,PEG2,PEG3);
return 0;
}
void move(int n,char source,char destination,char spare)
{
if(n<=1)
cout<<"Move the top disk form "<<source<<" to "
<<destination<<endl;
else
{
move(n-1,source,spare,destination);
move(1,source,destination,spare);
move(n-1,spare,destination,source);
}
}
[此贴子已经被作者于2007-7-2 22:49:47编辑过]