Tuesday, November 5, 2013

Code : Famous Three Jug Problem

#include <iostream>

int gcd(int a, int b)
{
    if (b == 0) {
        return a;
   }
    return gcd(b, a % b);
}

bool check (int a, int b, int c)
{
    if (c > a && c > b) {
        return false;
    }
     //if c is multiple of HCF of a and b, then possible
    if ( c % gcd(a,b) == 0) {
        return true;
    }
    //if c is not multiple of HCF of a and b, then not possible
    return false;
}
using namespace std;

int main ()
{
    int a, b, c, t;
    cin >> t;
    while (t--) {
        cin >> a >> b >> c;
        if (check (a, b, c)) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
    return 0;
}

No comments:

Post a Comment