quadratic equation

Think:
PS :题目出自
“浪潮杯”山东省第八届ACM大学生程序设计竞赛

题目意思 就是判断 当且仅当 x 为整数时, a * x * x + b * x + c = 0成立;
所以只要判断 a b c 三者对应的解即可;
一: a == 0 && b == 0 此时 若 c == 0 则 x 属于 R 所以不成立, 若 c != 0 则 无论 x 取何值都不成立 所以 命题成立;

二: a == 0 此时 是一元一次方程, 所以只要 用 int 来储存 解出的结果x 然后进行 等式判断即可

三:一元二次方程时, 求出 判别式, 然后直接暴力 求整数解的个数 然后对应判断即可;

四:一元二次方程 && 判别式 < 0 此时无论x 取何值 都不成立 , 所以命题为真

Problem Description

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

Hint
Author
“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

#include<bits/stdc++.h>
using namespace std;
int main()
 {
   int T;
   int a, b, c;
   int x;
   while(~scanf("%d", &T))
    {
       while(T --)
        {
           int flag = 0;
           scanf("%d %d %d", &a, &b, &c);

           if (a == 0 && b == 0 && c == 0)
            printf("NO\n");
            else
            if (a == 0 && b == 0 && c != 0)
            printf("YES\n");
            else
            if (a == 0)
               {
                  int k = -c / b;
                  if (a * k * k + b * k + c == 0)
                     printf("YES\n");
                     else
                     printf("NO\n");
               }
             else
               if (b * b - 4 * a * c >= 0)
                  {
                     for (x = -100; x <= 100; x ++)
                        {
                           if (a * x * x + b * x + 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");

        }
    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值