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

题意:

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

分析:

判断出来所有的可能性:

abc描述
000x为任意实数,eg.x = 0.1 使得方程为0,但是x不是整数
00!0对于任何x,方程都不为0,所以该命题为真
0!0!0bx + 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值