Problem Description
有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程。
例如:
x + y = 9,x * y = 15 ? 找不到这样的整数x和y
1+4=5,14=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
解题思路
其实这道题目超级简单,就是一个数学问题
x + y = n,x * y = m,两个联例,就可以解得 x=(n±√(n^2-4*m)),最后判断一下x是否为整数即可。这道题目的难点是判断整数,有很多种方法,我借鉴了网上的其中一种。
https://blog.youkuaiyun.com/zer1123/article/details/56039477.
代码
#include <iostream>
#include <math.h>
using namespace std;
int main(){
int n,m;
while(cin>>n>>m&&(n||m)){
double tmp=n*n-4*m;
if(tmp>=0){
double a1=(n+sqrt(tmp))/2;
double a2=(n-sqrt(tmp))/2;
if((a1-(int)a1==0)||(a2-(int)a2==0))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
else
cout<<"No"<<endl;
}
return 0;
}