x+y=n; x*y=m; 给定n,m,判断是否存在整数解.
我一开始想到的是韦达定理, 假设x,y是一元二次方程的两解,则x+y=-b/a=n,x*y=c/a;
而b^2-4ac等价于b^2/a^2-4c/a, 这样就能判断是否有解了, 可这题需要的是整数解.
所以还需要一些步骤判断是否是整数
x=-b+sqrt(b^2-4ac)/2a , 等价于 2x=n+sqrt(b^2-4ac)/a;
等价于n+sqrt(b^2-4ac)为偶数,
Problem Description
有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程。
例如:
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
例如:
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
Input
输入数据为成对出现的整数n,m(-10000<n,m<10000),它们分别表示整数的和与积,如果两者都为0,则输入结束。
Output
只需要对于每个n和m,输出“Yes”或者“No”,明确有还是没有这种整数就行了。
Sample Input
9 15 5 4 1 -56 0 0
Sample Output
No Yes Yes
#include"stdio.h"
#include"math.h"
#include"stdlib.h"
int main()
{
int n,m,t,x,x1,x2;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
t=n*n-4*m;
if(t<0)
{
printf("No\n");
continue;
}
x=(int)sqrt(t);
if(x*x!=t)
{
printf("No\n");
continue;
}
x1=n+x;
x2=n-x;
if(x1%2!=0||x2%2!=0)
printf("No\n");
else
printf("Yes\n");
}
}