quadratic equation
Time Limit: 2000MS Memory Limit: 131072KB
Problem Description
With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, if a⋅+b⋅x+c=0, then x is an integer."
Input
The first line contains only one integer T(1≤T≤2000), which indicates the number of test cases.
For each test case, there is only one line containing three integers a,b,c(−5≤a,b,c≤5).
Output
or each test case, output “YES
” if the statement is true, or “NO
” if not.
Example Input
3
1 4 4
0 0 1
1 3 1
Example Output
YES
YES
NO
判断是否有非整数解,如果有就是no,否则yes
当时做题的时候一直没有搞明白这个题,一直WA,0 0 1这个式子根本不成立,说明没有非整数解所以是yes
还有就是0 0 0 所有的数都符合这个式子 所以是no
其他的就分情况判断就行了
判断一元二次方程的整数解也有点套路 当时做题一直都是求出来之后double和int比较的
#include <iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<cmath>
using namespace std;
int main(){
int a,b,c,t;
double d;
cin>>t;
while(cin>>a>>b>>c)
{
bool flag=false;
if(a==0&&b==0&&c!=0)
flag=true;
else if(a==0&&b==0&&c==0)
flag=false;
else if(a==0&&b!=0&&c!=0)
{
if(c%b==0)
flag=true;
}
else if(a==0&&c==0&&b!=0)
flag=true;
else{
d=b*b-4*a*c;
int dd=sqrt(d);
if(d<0)
flag=true;
else{
if(fabs(dd-sqrt(d))<1e-9)
if((-b+dd)%(2*a)==0&&(-b-dd)%(2*a)==0)
flag=true;
}
}
if(flag==true)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}