Advertisement
rootUser

toh recursion (2)

May 26th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std ;
  4.  
  5.  
  6. void Hanoi(int n,int i,int j,int k){
  7.     if(n==1){
  8.         cout<<"Put Disk from "<<i<<" to "<<k<<endl;
  9.         return ;
  10.     }
  11.     else{
  12.         Hanoi(n-1,i,k,j);
  13.         Hanoi(1,i,j,k);
  14.         Hanoi(n-1,j,i,k);
  15.     }
  16. }
  17.  
  18.  
  19. int main(void){
  20.     int n;
  21.     while(true){
  22.         cout<<"Enter 0 for stop"<<endl;
  23.         cout<<"Enter the number of disks : "<<endl;
  24.         cin>>n;
  25.         if(!n) break ;
  26.         Hanoi(n,1,2,3);
  27.     }
  28.     return 0;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement