1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
void move(int count, int n1, int n3, int n2);
int main()
{
std::cout<<"Enter the number of disks: ";
int num;
std::cin >> num;
std::cout << "Calling move("
"count = " << num
<< ", n1 = 1"
", n3 = 2"
", n2 = 3"
")...\n";
move(num, 1, 2, 3);
return 0;
}
void move(int count, int n1, int n3, int n2)
{
if (count > 0)
{
std::cout << "Calling move(" << count - 1
<< ", " << n1
<< ", " << n2
<< ", " << n3
<< ")...\n";
move(count-1, n1, n2, n3);
std::cout << "--> Moving disk from " << n1 << " to " << n3 << ".\n";
std::cout << "Calling move(" << count - 1
<< ", " << n2
<< ", " << n3
<< ", " << n1
<< ")...\n";
move(count-1, n2, n3, n1);
}
}
| |