quadratic equation(蕴含式)

山东17省赛F:http://exam.upc.edu.cn/problem.php?cid=1207&pid=5

这题考查的知识点就是蕴含式,就是问你

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

别看题目短,其实它考虑的情况真的很多,让命题成立。对于所有的X    如果......则....

这个就是蕴含式如果        a⋅x2+b⋅x+c=0    则    x为整数。

表达出来是        a⋅x2+b⋅x+c=0        ->    x属于整数。

大家这道蕴含式可以前提为假,则这个命题就可以为真。

那么我们可以进行对合法情况进行枚举,前提为真时,实事求是地把情况枚举出来。

最后把前提为假的直接就输出YES即可。。。代码参考当时别人做的博客。尊重原创。


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        double a,b,c;
        scanf("%lf%lf%lf",&a,&b,&c);
        if(a==0){
            if(b==0&&c==0){
                printf("NO\n");
            }else if(b==0){
                printf("YES\n");
            }else if(-c/b==int(-c/b)){
                printf("YES\n");
            }else{
                printf("NO\n");
            }
        }else if(b*b-4*a*c>=0){
            int x=0;
            for(int i=-10;i<=10;i++){
                if(a*i*i+b*i+c==0){
                    x++;
                }
            }
            if((x==2&&b*b-4*a*c>0)||(x==1&&b*b-4*a*c==0)){
                printf("YES\n");
            }else{
                printf("NO\n");
            }
        }else{
            printf("YES\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/z_sea/article/details/80224352
今日推荐