zoukankan      html  css  js  c++  java
  • 2017年ACM第八届山东省赛F题:quadratic equation(离散数学蕴含式)

    F题:quadratic equation

    时间限制: 2 秒  内存限制: 128 MB  |  提交: 277  解决: 44 

    题目描述

    With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, if ax2+bx+c=0, then x is an integer."

    输入

    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).

    输出

    or each test case, output “YES” if the statement is true, or “NO” if not.

    样例输入

    3
    1 4 4
    0 0 1
    1 3 1
    

    样例输出

    YES
    YES
    NO
    题意:判断"For any x, if ax2+bx+c=0, then x is an integer."是否成立
       前件:For any x, if ax2+bx+c=0成立 (方程有解)
       后件:then x is an integer.成立 (解是整数)
       离散数学 p 为前件 q 为后件 的蕴含式 只有前件为 T 后件 为 F 时事件才为 F
    #include <cstdio>
    #include <cmath>
     
    bool judge(int a , int b , int c){
         
        if(a==0){
            if(b==0){
                if(c==0){
                    return false ; // 此时x可以取 任何值 不成立
                } 
                return true ;  
            } else if(c%b==0){
                return true ;  
            } 
            return false ; // 
        } 
        double d = b*b - 4 * a * c ; 
        int D = (int)sqrt(d) ;
        if(d < 0){
            return true ; 
        }
      
        if(fabs(D - sqrt(d)) < 0.00000001){
         
            if((0-b+D)%(2*a)==0 ){
                 
                if((0-b-D)%(2*a)==0)
                    return true ; 
            }
             
             
        }
         
        return false ; 
    }
     
    int main(){
        int t ; 
        int a , b , c ; 
        scanf("%d" , &t) ; 
        while(t--){
            scanf("%d%d%d" , &a , &b , &c) ; 
              
            if(judge(a , b , c)){
                printf("YES
    ") ; 
            } else {
                printf("NO
    ") ; 
            }
         
        }
     
        return 0 ; 
    }
    
    #include<stdio.h>
    #include<math.h>
    int main(void)
    {
        int T, a, b, c;
        double x;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d%d%d", &a, &b, &c);
            if(a==0 && b==0 && c==0)
                printf("NO
    ");
            else if(a==0 && b==0)
                printf("YES
    ");
            else if(a==0)
            {
                if(c%b==0)
                    printf("YES
    ");
                else
                    printf("NO
    ");
            }
            else if(b*b-4*a*c<0)
                printf("YES
    ");
            else
            {
                x = sqrt(1.0*b*b-4*a*c);
                if((-b+x)/(2*a)-(int)(-b+x)/(2*a)==0 && (-b-x)/(2*a)-(int)(-b-x)/(2*a)==0)
                    printf("YES
    ");
                else
                    printf("NO
    ");
            }
        }
        return 0;
    }


  • 相关阅读:
    HTTP常用的动词
    mysql5.7安装与主从复制
    Linq to XML 之XElement的Descendants方法的新发现
    SQL中的内连接外连接和交叉连接是什么意思?
    LINQ TO SQL ——Group by
    分布式Web服务器架构
    基于socket的客户端和服务端聊天机器人
    linq to sql (Group By/Having/Count/Sum/Min/Max/Avg操作符)
    关于301、302、303重定向的那些事
    手写async await的最简实现
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7631563.html
Copyright © 2011-2022 走看看