public class Solution {
public boolean canMeasureWater(int x, int y, int z) {
if(x+y<z)
return false;
if(x==z||x==z||x+y==z)
return true;
if(x<y)
{
int temp=x;
x=y;
y=temp;
}
return z%gcd(x,y)==0;//Bézout's identity x y 为整数 d 为x y的最大公约数 ,则 d=ax+by a b为整数
}
private int gcd(int x,int y)// 使用辗转相除法求出最大公约数
{
while(y!=0)
{
int temp=y;
y=x%y;
x=temp;
}
return x;
}
}
365. Water and Jug Problem
最新推荐文章于 2018-12-09 12:19:18 发布