端午节月赛-山东省第八届省赛 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+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
3
1 4 4
0 0 1
1 3 1
Example Output
YES
YES
NO
题意:
告诉你整数a, b, c, 判断命题“如果 a + b x + 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值