山东省第八届ACM省赛 F 题(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,让你判断这个逻辑公式对错;

思路:只要有x在a*x*x+b*x+c==0这个式子成立,那么x一定是整数,即:

p   q    p->q

0   0      1
0   1      1
1   0      0
1   1      1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int t;
double a,b,c;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf%lf",&a,&b,&c);  //一开始把a,b,c弄成double型,省的之后转换类型了;
        if(a==0&&b==0)
        {
            if(c==0) printf("NO\n");  //当a==b==c==0时,所有的x都符合,所有是NO;
            else printf("YES\n");  //c!=0时,前式为错,不管后面对错,都是对的;
        }
        else if(a==0)
        {
            if((-c/b)==(int)(-c/b)) printf("YES\n");  //直接判定x是不是整数;
            else printf("NO\n");
        }
        else if(b*b-4*a*c>=0)
        {
            int flag=0;
            for(int i=-5; i<=5; i++)  //找到x为整数的个数;
            {
                if(a*i*i+b*i+c==0)
                {
                    flag++;
                }
            }
            if(b*b-4*a*c==0&&flag==1) printf("YES\n");
            else if(b*b-4*a*c>0&&flag==2) printf("YES\n");
            else printf("NO\n");
        }
        else printf("YES\n");  //当b*b-4*a*c<0时,前式不成了,所以一直是对的;
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值