1209: Three Jugs
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 245 Solved: 37
[ Submit][ Status][ Web Board]
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
HINT
Source
找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;
}

本文介绍了一个经典的数学问题——利用三个不同容量的水壶获取精确水量的方法。文章提供了详细的算法思路,包括如何通过填充、倒空及相互倾倒水来实现目标水量,并给出了具体的代码实现。
1556

被折叠的 条评论
为什么被折叠?



