“浪潮杯”山东省第八届ACM大学生程序设计竞赛F

来源:互联网 发布:焦作优易网络 编辑:程序博客网 时间:2024/05/16 14:02

quadratic equation

Time Limit: 2000MS Memory Limit: 131072KB
Submit Statistic

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

31 4 40 0 11 3 1

Example Output

YESYES

NO

判断是否有非整数解,如果有就是no,否则yes

当时做题的时候一直没有搞明白这个题,一直WA,0 0 1这个式子根本不成立,说明没有非整数解所以是yes

还有就是0 0 0 所有的数都符合这个式子 所以是no

其他的就分情况判断就行了

判断一元二次方程的整数解也有点套路 当时做题一直都是求出来之后double和int比较的

01#include <iostream>
02#include<string.h>
03#include<string>
04#include<algorithm>
05#include<stdio.h>
06#include<cmath>
07using namespace std;
08int main()
09{
10    int a,b,c,t;
11    double d;
12    cin>>t;
13    while(cin>>a>>b>>c)
14    {
15        bool flag=false;
16        if(a==0&&b==0&&c!=0)
17            flag=true;
18        else if(a==0&&b==0&&c==0)
19            flag=false;
20        else if(a==0&&b!=0&&c!=0)
21                {
22                    if(c%b==0)flag=true;
23                }
24        else if(a==0&&c==0&&b!=0)
25                flag=true;
26        else {
27                d=b*b-4*a*c;
28                int dd=sqrt(d);
29                if(d<0)
30                    flag=true;
31                else {
32       if(fabs(dd-sqrt(d))<1e-9)
33        if((-b+dd)%(2*a)==0&&(-b-dd)%(2*a)==0)flag=true;                 
34}
35            }
36    if(flag==true)
37        cout<<"YES"<<endl;
38    else
39        cout<<"NO"<<endl;
40    }
41    return 0;
42}
43 
44/***************************************************
45User name: 战神
46Result: Accepted
47Take time: 4ms
48Take Memory: 204KB
49Submit time: 2017-05-12 16:38:47
50****************************************************/

1 0
原创粉丝点击