#include <iostream>
using namespace std;
void hanoi(int n, char a, char b, char c)
{
if(1 == n) //the last step: from a to c
{
cout<<"Move "<<n<<" sheet from "<<a<<" ---> "<<c<<endl;
}
else
{
hanoi(n-1, a, c, b); //use b as a temp one and it is a recursion
cout<<"Move "<<n<<" sheet from "<<a<<" ---> "<<c<<endl;
hanoi(n-1, b, a, c); //remember how to 'cout'
}
}
int main()
{
int n;
cout<<"Enter a number:";
cin>>n;
hanoi(n,'a', 'b', 'c');
return 0;
}
//1: g++ hanoi.cpp -o test
//2: ./test
using namespace std;
void hanoi(int n, char a, char b, char c)
{
if(1 == n) //the last step: from a to c
{
cout<<"Move "<<n<<" sheet from "<<a<<" ---> "<<c<<endl;
}
else
{
hanoi(n-1, a, c, b); //use b as a temp one and it is a recursion
cout<<"Move "<<n<<" sheet from "<<a<<" ---> "<<c<<endl;
hanoi(n-1, b, a, c); //remember how to 'cout'
}
}
int main()
{
int n;
cout<<"Enter a number:";
cin>>n;
hanoi(n,'a', 'b', 'c');
return 0;
}
//1: g++ hanoi.cpp -o test
//2: ./test