quadratic equation (SDIBT 3268)(数学解方程)

 

Description

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

Sample Input

3
1 4 4
0 0 1
1 3 1

Sample Output

YES
YES
NO

题意:给定三个整数 a, b, c,判断方程 ax^2+bx+c=0的解是否全部为整数。

注意:当此方程无解的时候,输出  YES ;当x可以为任意数时,输出 NO ,因为输出YES的前提是所有的x都必须为整数,而当x为任意数时,x也可以不是整数。

注意考虑a=0,b=0,c=0的情况!当时因为这个地方W了无数次(T﹏T)。

代码如下 :

 

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
 
int main()
{
    int t,a,b,c;
    cin >> t;
    while(t--)
    {
        cin >> a >> b >> c;
        if(a==0)
        {
            if(b==0)
            {
                if(c==0)
                    cout << "NO" << endl;      //此情况x为任意数,不全是整数,输出 NO
                else
                    cout << "YES" << endl ;    //此情况无解,输出 YES
            }
            else
            {
                double p;
                if((-c)%b==0)
                    cout << "YES" << endl ;    // 有一个解且为整数,YES
                else
                    cout << "NO" << endl;      // 有一个解但不是整数,NO
            }
        }
 
        else
        {
            int k;
            k=b*b-4*a*c;                       //判别式 
            if(k<0)
                cout << "YES" <<endl;
            if(k==0)
            {
                double p;
                p=(-b+sqrt(k))/2.0/a;          //求根公式
                if(p==(int)p )                 //判断是否为整数
                    cout << "YES" <<endl ;
                else
                    cout << "NO" << endl ;
            }
            if(k>0)
            {
                double c1,c2;
                c1=(-b+sqrt(k))/2.0/a;        
                c2=(-b-sqrt(k))/2.0/a;
                if(c1==(int)c1&&c2==(int )c2)  //判断是否为整数
                    cout << "YES" << endl;
                else
                    cout << "NO" << endl ;
            }
        }
    }
    return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值