Three Jugs(辗转相除法求GCD)
来源:互联网 发布:域名绑定ip 编辑:程序博客网 时间:2024/05/29 16:41
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
HINT
找3个数的最大公约数
#include<stdio.h>
unsigned
long
long
gcd(unsigned
long
long
a,unsigned
long
long
b)
{
if
(a==0)
return
b;
else
return
gcd(b%a,a);
}
int
main()
{
int
t;
unsigned
long
long
a,b,c,ans;
//记得改回long long
while
(
scanf
(
"%d"
,&t)!=EOF)
{
while
(t--)
{
scanf
(
"%lld %lld %lld"
,&a,&b,&c);
ans=gcd(a,gcd(b,c));
printf
(
"%lld\n"
,ans);
}
}
return
0;
}
虽然并没有明白为什么是最大公约数。。。
这里是GCD的一点知识:
求法:辗转相除法 辗转相除法是求两个自然数的最大公约数的一种方法,也叫欧几里德算法。
- Three Jugs(辗转相除法求GCD)
- 辗转相除法GCD求最大公约数
- 最大公约数(gcd)辗转相除法
- 关于辗转相除法求gcd的证明
- 关于辗转相除法求gcd的证明
- gcd&&lcm 辗转相除法
- 辗转相除法求最大公约数(最小公倍数)
- 求最大公约数(辗转相除法)
- 求最大公约数(辗转相除法)
- 求最大公约数-辗转相除法(Java)
- 求最大公约数(辗转相除法)
- CSU1209-Three Jugs-GCD
- 辗转相除法求最大公约数
- 辗转相除法求最大公约数
- 辗转相除法求最大公约数
- 辗转相除法求最大公约数
- 求最大公约数-辗转相除法
- 辗转相除法求最大公约数
- POJ Common Subsequence
- jquery 对 Json 的各种遍历
- 发送结构体数据转序
- vim配置
- FreakZ学习笔记:FreakZ通信网络发送过程详解
- Three Jugs(辗转相除法求GCD)
- 不相交集ADT
- CDN(内容分发网络)技术原理
- mac 终端 常用命令
- 关于短信拦截、电话拦截的一些注意点
- 已成功与服务器建立连接,但是在登录过程中发生错误。 (provider: 共享内存提供程序, error: 0
- 二维数组作为函数参数深度详解
- 函数的递归调用(阶乘函数&斐波那契数列)
- 二叉树中的三种遍历方式