quadratic equation
Time Limit: 2000 ms
Memory Limit: 131072 KiB
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.
Sample Input
3 1 4 4 0 0 1 1 3 1
Sample Output
YES YES NO
Hint
Source
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)
给出a,b,c,让你判断这个逻辑公式对错;
思路:只要有x在a*x*x+b*x+c==0这个式子成立,那么x一定是整数,即:
p q p->q
0 0 1
0 1 1
1 0 0
1 1 1
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;
int shu(int a,int b,int c){
if(a==0)
{
if(b==0)
{
if(c==0)
{
return 0;
}else{
return 1;
}
}else{
/*if(c==0){
return 1;
}else{*/
if(c%b==0){
return 1;
}
else{
return 0;
}
}
//}
}else{
double s;
s=b*b-4*a*c;
if(sqrt(s)>=0){
double w1=(-b+sqrt(s))/(2*a); //求根公式写错了,改了很多次
int w2=(int)w1;
double r1=(-b-sqrt(s))/(2*a);
int r2=(int)r1;
if(w1-w2!=0 || r1-r2!=0){
return 0;
}
else{
// if((-1*b-(int)sqrt(s))%(2*a)!=0 ||(-1*b+(int)sqrt(s))%(2*a)!=0){
// return 0;
// }
//else{
return 1;
//}
}
}else{
return 1;
}
}
}
int main(){
int a,b,c;
int t,x,i;
int flag;
scanf("%d",&t);//不要用while读入
for(i=0;i<t;i++){
scanf("%d%d%d",&a,&b,&c);
flag=shu(a,b,c);
if(flag){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}