HDU integer solution

Hangzhou Electric integer solution

Integer solution

Problem Description
There are two integers which add up to an integer equal to ride up and another integer, which in the end is true or false, this is an integer in the end it exists or not, it is a bit not sure, you can quickly answer it? It seems only through programming.
For example:
x = Y +. 9, Y = x * 15 no such integers x and Y?
. 1 5,1 + 4 = 4 = 4, therefore, add up to 5, up by two integer equal to 4 1 and 4
7 + (- 8) = - 1,7
(-8) = - 56, therefore, add up to -1, multiply the two integer equal to 7 and -8 to -56

Input
input data is integer pairs of n, m (-10000 <n, m <10000), which represent the sum and product of integers, if both are 0, the input ends.

Output
only for each n and m, output "Yes" or "No", clearly there is still no such integer on the line.

Sample Input
9 15
5 4
1 -56
0 0

Sample Output
No
Yes
Yes

Author
qianneng

#include<bits/stdc++.h>
using namespace std;


bool YesOrNo(int n,int m)
{

    int a,b;
    if(m<0)
    {
        b=-m;//如果异号,则先转为正的
    }
    else b=m;



    for(int i=1; i<(sqrt(b)+1); i++)//从-sqrt(m)~aqrt(m),可能的数就在这其中。所以从这里面找,一个个的,而且范围还算小,不超时,范围如果取大可能超时
    {
        a=m/i;
        if(a*i==m&&a+i==n)
        {
            //cout<<"Yes"<<endl;
            return 1;
        }
    }

    for(int i=-1; i>-(sqrt(b)-1); i--)
    {
        a=m/i;
        if(a*i==m&&a+i==n)
        {
            //cout<<"Yes"<<endl;
            return 1;
        }
    }
    return 0;

}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(m==0&&n==0)
        return 0;

        else
        {
            if(YesOrNo(n,m)){

                cout<<"Yes"<<endl;
            }
            else cout<<"No"<<endl;


        }




    }



}

Published 18 original articles · won praise 0 · Views 265

Guess you like

Origin blog.csdn.net/qq_42815711/article/details/104212229