**
水壶问题
**
在leetcode上写的第一道题,利用了裴蜀定理,这个写起来比较简洁,其他方法有时间再研究
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h> //C语言中是没有bool变量的,需要引入此头文件
bool canMeasureWater(int x,int y,int z);
int gcd(int x, int y);
//水壶问题,用到了裴蜀定理
bool canMeasureWater(int x, int y, int z)
{
int m = gcd(x, y);
if (z == 0)
return true;
else if (z >= 1 && z <= (x + y))
{
if (z % m == 0)
{
return true;
}
}
return false;
}
//求最大公约数
int gcd(int x, int y)
{
int z;
while (y != 0)
{
z = x % y;
x = y;
y = z;
}
return x;
}
int main()
{
int x, y, z;
printf("请输入三个整数:");
scanf_s("%d%d%d", &x, &y, &z); //微软的输入,更加安全的一种写法
bool b;
b = canMeasureWater(x, y, z);
if (b)
printf("\ntrue");
else
printf("\nfalse");
printf("\n");
system("pause");
return 0;
}