整数解
题目描述
有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程。
例如:
x + y = 9,x * y = 15 ? 找不到这样的整数x和y
1+4=5,1*4=4,所以,加起来等于5,乘起来等于4的二个整数为1和4
7+(-8)=-1,7*(-8)=-56,所以,加起来等于-1,乘起来等于-56的二个整数为7和-8
Iutput
输入数据为成对出现的整数n,m(-10000
Output
只需要对于每个n和m,输出“Yes”或者“No”,明确有还是没有这种整数就行了。
Sample Input
9 15
5 4
1 -56
0 0
Sample Output
No
Yes
Yes
思路:
一元二次方程的整数解问题;
代码
#include <cstdio>
#include <math.h>
using namespace std;
int main()
{
int flag;
double a,b,d,x1,x2;
while(scanf("%lf%lf",&a,&b),a||b){
flag=1;
d=(a+b)*(a+b)-4*a*b;
if(d<=0)
flag=0;
else {
x1=(a+b+sqrt(d))/2;
x2=(a+b-sqrt(d))/2;
if(x1!=(int)x1||x2!=(int)x2)
flag=0;
}
if(flag)
printf("Yes\n");
else printf("No\n");
}
return 0;
}