ACM:Y: Three Jugs
Description
We have three jugs A, B, C without any calibration, and an infinite supply of water. There are three types of actions that you can use:
(1) Fill a jug.
(2) Empty a jug.
(3) Pour from one jug to another.
Pouring from one jug to another stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons, B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.
Now you need to calculate the minimum accurate gallons of water we can get by using the three jugs.
Input
There is an integer T (1 <= T <= 200) in the first line, means there are T test cases in total.
For each test case, there are three integers a, b, c (1 <= a, b, c <= 10^18) in a line, indicate the capacity (unit: gallon) of the three jugs.
Output
For each test case, you should print one integer in a line, indicates the minimum accurate gallons of water we can get by using the three jugs.
Sample Input
2 3 6 9 6 10 15
Sample Output
3 1
注:仔細觀察,其實就是求最大公約數的問題,另外注意a,b,c的數據範圍,要用long long類型。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define LL long long
//最大公約數
LL gcd(LL a,LL b)
{
// if(b==0)
return b==0? a:gcd(b,a%b);
}
int main()
{
int T;
LL a,b,c;
cin>>T;
while(T--)
{
cin>>a>>b>>c;
cout<<gcd(a,gcd(b,c))<<endl;
}
return 0;
}