这个题困扰了我长达1年多,终于在今天下午用两个小时理清楚啦
要注意的有以下几点:
1.a=b=c=0时 因为x有无穷种答案,所以不对
2.注意精度问题
3.b^2-4ac<0时也算对
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
1 #include <iostream> 2 #include <math.h> 3 4 using namespace std; 5 6 double _abs(double a) 7 { 8 if (a < 0) 9 return -a; 10 return a; 11 } 12 13 int main() 14 { 15 ios::sync_with_stdio(false); 16 int t; 17 cin >> t; 18 while (t--) 19 { 20 double a, b, c; 21 cin >> a >> b >> c; 22 if (!a && !b) 23 { 24 if (!c) 25 cout << "NO" << endl; 26 else 27 cout << "YES" << endl; 28 } 29 else if (!a && b) 30 { 31 if (_abs(c/b - (int)(c/b)) > 0.0000001) 32 cout << "NO" << endl; 33 else 34 cout << "YES" << endl; 35 } 36 else 37 { 38 if ((b * b - 4 * a * c) >= 0) 39 { 40 double x1 = (-b + sqrt(b*b - 4 * a*c)) / (2 * a); 41 double x2 = (-b - sqrt(b*b - 4 * a*c)) / (2 * a); 42 //cout << x1 << ends << x2 << endl; 43 if (_abs(x1 - (int)x1) > 0.0000001 || _abs(x2 - (int)x2) > 0.0000001) 44 cout << "NO" << endl; 45 else 46 cout << "YES" << endl; 47 } 48 else 49 cout << "YES" << endl; 50 } 51 } 52 53 return 0; 54 }