HDU整数解

杭电 整数解

整数解

Problem Description
有二个整数,它们加起来等于某个整数,乘起来又等于另一个整数,它们到底是真还是假,也就是这种整数到底存不存在,实在有点吃不准,你能快速回答吗?看来只能通过编程。
例如:
x + y = 9,x * y = 15 ? 找不到这样的整数x和y
1+4=5,14=4,所以,加起来等于5,乘起来等于4的二个整数为1和4
7+(-8)=-1,7
(-8)=-56,所以,加起来等于-1,乘起来等于-56的二个整数为7和-8

Input
输入数据为成对出现的整数n,m(-10000<n,m<10000),它们分别表示整数的和与积,如果两者都为0,则输入结束。

Output
只需要对于每个n和m,输出“Yes”或者“No”,明确有还是没有这种整数就行了。

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;


        }




    }



}

发布了18 篇原创文章 · 获赞 0 · 访问量 265

猜你喜欢

转载自blog.csdn.net/qq_42815711/article/details/104212229