leetcode:365. 水壶问题(数学)

题目:

在这里插入图片描述

分析:

显然,就是给x,y,|x-y|配倍数,找是否有正整数的解。

再进一步,给x,y配倍数,找是否有整数的解。此时系数可以为负数了。
紫书上的数学模块有提到过相关的结论。

在这里插入图片描述

代码:

int main()
{
 if(x + y < z) return 0;
 if(z==0) return 1;
 if(x==0&&y==0)
 {
  return 0;
 }
 if(x==0)
 {
  if(z%y==0) return 1;
  return 0;
 }
 if(y==0)
 {
  if(z%x==0) return 1;
  return 0;
 }
  if(z%gcd(x,y)) return 1;
  return 0;
}
发布了196 篇原创文章 · 获赞 126 · 访问量 4657

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/104540608