Advertisement
vkichukova

Untitled

Jan 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int min(int a, int b)
  7. {
  8. return a < b ? a : b;
  9. }
  10.  
  11. int gcd(int n, int m)
  12. {
  13. if (m <= n && n % m == 0)
  14. return m;
  15. if (n < m)
  16. return gcd(m, n);
  17. else
  18. return gcd(m, n % m);
  19. }
  20.  
  21. void countSteps(int a, int b, int n, int &count)
  22. {
  23. int capacityA = 0;
  24. int capacityB = 0;
  25.  
  26. if (a == n || b == n)
  27. return ;
  28.  
  29. if (capacityA == 0)
  30. {
  31. cout << "enter 1\n";
  32. capacityA = a;
  33. count++;
  34. }
  35.  
  36. if (capacityB == b)
  37. {
  38. cout << "enter 2\n";
  39. capacityB = 0;
  40. count++;
  41. }
  42. if(capacityA != 0 && capacityB != b)
  43. {
  44. cout << "enter 3\n";
  45. int temp = min(capacityA, b - capacityB);
  46. count++;
  47. countSteps(capacityA - temp, capacityB + temp, n, count);
  48. }
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54. int jug1, jug2, result;
  55. int count = 0;
  56. do
  57. cin >> jug1 >> jug2 >> result;
  58. while (jug1 < 1 || jug1 > 20 || jug2 < 1 || jug2 > 20);
  59.  
  60. if (jug1 < jug2)
  61. swap(jug1, jug2);
  62.  
  63. int x = gcd(jug1, jug2);
  64. if (result <= jug1 || result <= jug2 && result % x == 0)
  65. {
  66. countSteps(jug1, jug2, result, count);
  67. cout << count << endl;
  68. }
  69. else
  70. cout << "-1" << endl;
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement