fork(4) download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void hanoi(int n, int beg, int aux, int end)
  6. {
  7. if (n == 1)
  8. {
  9. cout << beg << " " << end << endl;
  10. }
  11. else
  12. {
  13. hanoi(n - 1, beg, end, aux);
  14. hanoi(1, beg, aux, end);
  15. hanoi(n - 1, aux, beg, end);
  16. }
  17. }
  18.  
  19. int main()
  20. {
  21. int n;
  22. cout << "Ile dyskow podac: " << endl;
  23. cin >> n;
  24.  
  25. while (n > 1)
  26. {
  27. cout << "n = " << n << endl;
  28. if (n % 2 == 0)
  29. hanoi(n - 1, 1, 3, 2);
  30. else
  31. hanoi(n - 1, 2, 3, 1);
  32. --n;
  33. }
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3300KB
stdin
6
stdout
Ile dyskow podac: 
n = 6
1 2
1 3
2 3
1 2
3 1
3 2
1 2
1 3
2 3
2 1
3 1
2 3
1 2
1 3
2 3
1 2
3 1
3 2
1 2
3 1
2 3
2 1
3 1
3 2
1 2
1 3
2 3
1 2
3 1
3 2
1 2
n = 5
2 3
2 1
3 1
2 3
1 2
1 3
2 3
2 1
3 1
3 2
1 2
3 1
2 3
2 1
3 1
n = 4
1 2
1 3
2 3
1 2
3 1
3 2
1 2
n = 3
2 3
2 1
3 1
n = 2
1 2