chore(CPlusPlus): added tower of hanoi in recursion (#394)
parent
c72de71551
commit
f01e030335
|
@ -102,3 +102,6 @@
|
|||
2. [Prime Number](Maths/prime-check.cpp)
|
||||
3. [Prime Sieve](Maths/prime-sieve.cpp)
|
||||
4. [Fibonacci Series](Maths/fibonaccci-series.cpp)
|
||||
|
||||
# Recursion
|
||||
1. [Tower of Hanoi](Recursion/towerofHanoi.cpp)
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
void towerOfHanoi(int disks,char stand1,char stand2,char stand3)
|
||||
{
|
||||
if(disks==1)
|
||||
{cout<<"Move disk 1 from "<<stand1<<" to "<<stand3<<endl;
|
||||
return ;
|
||||
}
|
||||
towerOfHanoi(disks-1,stand1,stand3,stand2);
|
||||
cout<<"Move disk "<<disks<<" from "<<stand1<<" to "<<stand3<<endl;
|
||||
towerOfHanoi(disks-1,stand2,stand1,stand3);
|
||||
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int n;
|
||||
cin>>n;
|
||||
towerOfHanoi(n,'A','B','C');
|
||||
}
|
Loading…
Reference in New Issue