Advertisement
force1987

Hanoi

Apr 22nd, 2021
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void func(int count, int init[], int dest[], int buf[]) {
  5. if (count == 0) return;
  6. func(count - 1, init, buf, dest);
  7. int indexi = 0, indexd = 9;
  8. while (init[indexi] == 0)indexi++;
  9. while (dest[indexd] != 0)indexd--;
  10. swap (init[indexi], dest[indexd]);
  11. func(count - 1, buf, dest, init);
  12. }
  13.  
  14. int main() {
  15. setlocale(LC_ALL, "RUS");
  16. int rod1[10];
  17. for (int i = 0; i < 10; i++) {
  18. rod1[i] = i + 1;
  19. }
  20. int rod2[10]{};
  21. int rod3[10]{};
  22. cout << "Исходный стержень" << endl;
  23. for (int i = 0; i < 10; i++) {
  24. cout << rod1[i] << " ";
  25. }
  26. cout << endl;
  27. cout << "Конечный стержень" << endl;
  28. for (int i = 0; i < 10; i++) {
  29. cout << rod3[i] << " ";
  30. }
  31. cout << endl;
  32. cout << "Буферный стержень" << endl;
  33. for (int i = 0; i < 10; i++) {
  34. cout << rod2[i] << " ";
  35. }
  36. cout << endl<<endl;
  37. func(10, rod1, rod3, rod2);
  38. cout << "Исходный стержень" << endl;
  39. for (int i = 0; i < 10; i++) {
  40. cout << rod1[i] << " ";
  41. }
  42. cout << endl;
  43. cout << "Конечный стержень" << endl;
  44. for (int i = 0; i < 10; i++) {
  45. cout << rod3[i] << " ";
  46. }
  47. cout << endl;
  48. cout << "Буферный стержень" << endl;
  49. for (int i = 0; i < 10; i++) {
  50. cout << rod2[i] << " ";
  51. }
  52. cout << endl;
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement