端午节月赛-山东省第八届省赛 quadratic equation(多种情况,△的判断)

来源:互联网 发布:c 面向对象编程流程 编辑:程序博客网 时间:2024/06/07 13:45

Problem Description

With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, if a+bx+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

31 4 40 0 11 3 1

Example Output

YESYESNO
题意:
告诉你整数a, b, c, 判断命题“如果a+bx+c=0,那么 x 为整数"是否为真命题。

思路:
就是解一元二次方程组,多了1、判断是否为整数, 2、命题逻辑的知识点

注意点:
1、判断△<0 时,命题成立;
2、等价命题“如果 x 不是整数,那么等式不成立”
3、要分别考虑a = 0, b = 0, c = 0时的情况。


#include <cstdio>#include <iostream>#include <map>#include <algorithm>#include <math.h>#include <queue>#include <string>#include <cstring>using namespace std;int main(){    int T;    scanf("%d", &T);    while( T --)    {        double a, b, c;        double x1, x2;        cin >> a >> b >> c;        if(a != 0)        {                    double d = b * b - 4 *a *c;   //判断△是否小于0                    if(d >= 0)                    {                        d = sqrt(d);                        x1 = -b +d;                        x1 /= 2 * a;                        x2 = -b -d;                        x2 /= 2 *a;                    }                    else                        x1 = x2 = 1;        }        else if(b != 0)        {            x1 = -c / b;            x2 = -c / b;        }        else if(c != 0)        {            x1 = x2 = 1;        }        else            x1 = x2 = -0.5;        //cout << x1 <<x2;        int n1 = ceil(x1);   //判断整数的方法        int n2 = ceil(x2);        //cout << n1 << n2 << endl;        if(n1==x1 && n2 == x2)            cout << "YES" << endl;        else            cout << "NO" << endl;    }    return 0;}


阅读全文
0 0