Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int min(int a, int b)
- {
- return a < b ? a : b;
- }
- int gcd(int n, int m)
- {
- if (m <= n && n % m == 0)
- return m;
- if (n < m)
- return gcd(m, n);
- else
- return gcd(m, n % m);
- }
- void countSteps(int a, int b, int n, int &count)
- {
- int capacityA = 0;
- int capacityB = 0;
- if (a == n || b == n)
- return ;
- if (capacityA == 0)
- {
- cout << "enter 1\n";
- capacityA = a;
- count++;
- }
- if (capacityB == b)
- {
- cout << "enter 2\n";
- capacityB = 0;
- count++;
- }
- if(capacityA != 0 && capacityB != b)
- {
- cout << "enter 3\n";
- int temp = min(capacityA, b - capacityB);
- count++;
- countSteps(capacityA - temp, capacityB + temp, n, count);
- }
- }
- int main()
- {
- int jug1, jug2, result;
- int count = 0;
- do
- cin >> jug1 >> jug2 >> result;
- while (jug1 < 1 || jug1 > 20 || jug2 < 1 || jug2 > 20);
- if (jug1 < jug2)
- swap(jug1, jug2);
- int x = gcd(jug1, jug2);
- if (result <= jug1 || result <= jug2 && result % x == 0)
- {
- countSteps(jug1, jug2, result, count);
- cout << count << endl;
- }
- else
- cout << "-1" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement