quadratic equation
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
首先讲这道题就是判断蕴含式的真假
p -> q
如果前提为假,结果无论是什么,都是真(0->1,0->0都是真)
如果前提是真,结果是真,为真(1->1是真)
如果前提是真,结果是假,为假(1->0是假)
所以要注意的就是如果是二次方程,无解情况都是真的,也就是判别式小于0的时候,还有一点容易忽略的就是如果ab都是0
只剩下常数c,如果c=0说明前提是真,而x取任何值前提都满足,所以此时为假,而c取其他常数时,前提都为假,命题为真。
但是你以为这样你就能过了吗,不一定,他妈的老子交了10多次就是不过,到现在也不知道这道题还应该注意什么,只能粘网上代码了。
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
double a,b,c;
scanf("%lf%lf%lf",&a,&b,&c);
if(a == 0 && b == 0){
if(c == 0) printf("NO\n");//前提条件为1,后面结果为0(x可以不是整数)为假
else printf("YES\n");//前提条件为0,都是真
}
else if(a == 0){
if((-c / b) == (int)(-c / b))
printf("YES\n");
else
printf("NO\n");
}
else if(b * b - 4 * a* c >= 0){
int flag = 0;
for(int i = -5; i <= 5; i++){
if(a * i * i + b * i + c == 0)
flag++;
}
if(b * b - 4 * a * c == 0 && flag == 1)
printf("YES\n");
else if(b * b - 4 * a * c > 0 && flag == 2)
printf("YES\n");
else printf("NO\n");
}
else printf("YES\n");
}
return 0;
}