Advertisement
MouseyN1

Turnurile din Hanoi

Mar 7th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int k;
  4. void Hanoi(int n, int t1, int t2, int t3)
  5. {
  6.     if(n == 1)
  7.         cout << "Mutarea numarul " << ++k << '\n' << t1 << " -> " << t3 << '\n';
  8.     else
  9.     {      
  10.         Hanoi(n - 1, t1, t3, t2);
  11.         cout << '\n';
  12.         cout << "Mutarea numarul " << ++k << '\n' << t1 << " -> " << t3 << '\n'; // Mutam cel mai mare disc de pe t1 pe t3
  13.         Hanoi(n - 1, t2, t1, t3);   // Mutam cele n-1 discuri de pe t2 pe t3
  14.                                     // folosind ca intermediar tija t1
  15.     }
  16. }
  17.  
  18. int main(void)
  19. {
  20.     int n;
  21.     cout << "Dati numarul de discuri: ";
  22.     cin >> n;
  23.     Hanoi(n, 1, 2, 3);
  24.     return 0;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement