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;}
- CSU 1209 Three Jugs
- CSU - 1209 Three Jugs
- CSU 1209 Three Jugs (数论)
- 1209: Three Jugs
- COJ 1209: Three Jugs
- ACM:Y: Three Jugs
- CSU1209-Three Jugs-GCD
- CSU 1805 CSU 1805 Three Capitals
- Three Jugs(辗转相除法求GCD)
- Jugs
- Jugs
- Jugs
- Jugs
- CSU 1805: Three Capitals(BEST定理)
- CSU 1805 Three Capitals(BEST定理)
- CSU
- CSU
- CSU
- 如何安装fabric
- 《Java编程思想第四版》笔记---15章(1) 泛型编程基础
- heap最大(小)堆
- Javascript高级程序设计读书笔记——继承
- python opencv 显示直方图
- CSU - 1209 Three Jugs
- eclipse mars 及以上安装 velocity 插件
- UIView和CALayer的区别
- POJ2653->判断线段相交
- 多变异位自适应遗传算法(MMAdapGA)的算法原理、算法步骤和matlab实现
- Struts数据验证
- maven项目创建:cmd创建项目再导入eclipse开发工具
- hive 使用TEZ的安装配置
- 《Java编程思想第四版》笔记---15章(2)泛型类型擦除