CSU - 1209 Three Jugs

来源:互联网 发布:淘宝美工需要注意什么 编辑:程序博客网 时间:2024/06/04 20:07

题目:

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

23 6 96 10 15

Sample Output

31

首先要理解题目,其实就是直接求a,b,c的最大公约数就可以了。

而且只需要用可以求2个数的最大公约数的函数gcd即可,可以发现,调用2次gcd,效率也还是很高的。

代码:

#include<iostream>using namespace std;long long gcd(long long a, long long b){if (b == 0)return a;return gcd(b, a%b);}int main(){int n;cin >> n;long long a, b, c;while (n--){cin >> a >> b >> c;b = gcd(a, b);cout << gcd(b,c) << endl;}return 0;}

1 0
原创粉丝点击