山东省第八届ACM大学生程序设计竞赛 F题 quadratic equation 逻辑判断+数学题

题意:

给你一个命题:“For any x, if a x 2 + b x + c = 0 , then x is an integer.”对于任意x,如果 a x 2 + b x + c = 0 ,那么x是一个整数。
让你判断这个命题是真假,输出YES、NO;

分析:

判断出来所有的可能性:

a b c 描述
0 0 0 x为任意实数,eg.x = 0.1 使得方程为0,但是x不是整数
0 0 !0 对于任何x,方程都不为0,所以该命题为真
0 !0 !0 bx + c = 0; x = c/b,如果c/b是整数,x是整数,否则不是
!0 - - 一元二次方程,求根公式,判断x1,x2是否为整数

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#define debug cout<<"**********"<<endl;
#define ll long long
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
using namespace std;
const int maxn = 10000;
const int mod = 1e9+7;
int main()
{
    std::ios::sync_with_stdio(false);
    int T;
    cin>>T;
    double a,b,c;
    while(T--)
    {
        cin>>a>>b>>c;
        if(a == 0)
        {
            if(b == 0)
            {
                if(c == 0)
                {
                    no;
                }
                else
                {
                    yes;
                }
            }
            else
            {
                if((int)c%(int)b == 0)
                {
                    yes;
                }
                else
                {
                    no;
                }
            }
        }
        else
        {

            double k = b*b-4*a*c;
            if(k < 0)
            {
                yes;
            }
            else
            {
                double d = sqrt(k);
                double x1 = (-b+d)/(2*a);
                double x2 = (-b-d)/(2*a);
                int xx1 = x1;
                int xx2 = x2;
                if(x1 - xx1 ==0 && x2 - xx2 == 0)
                {
                    yes;
                }
                else
                {
                    no;
                }
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shensiback/article/details/80167860