zoukankan      html  css  js  c++  java
  • 杭电2092 整数解

    整数解

    Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 35239    Accepted Submission(s): 12413


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

      这题很简单,我是一次过的,然后大致依旧就是看看那有没有符合方程组x + y = num1,x * y = num2 的解。最主要的还是看有没有整数解,然后就利用初中的公式,x=-b+根号b^2-4ac/2a;幻化成编程语言就是(num1+pow((num1*num1-4*num2),0.5))/2.0

    然后就是判断x是不是整数,接下来奉上代码:

    #include <iostream>
    #include<math.h>
    #include <iomanip>
    #include<cstdio>
    #include<string>
    #include<map>
    #include<vector>
    #include<list>
    #include<algorithm>
    #include<stdlib.h>
    #include<iterator>
    #include<sstream>
    #include<string.h>
    using namespace std;
    
    
    int main()
    {
        double num1,num2;
        while(cin>>num1>>num2)
        {
            if(num1==0&&num2==0)
            {
                break;
            }
            int x,y;
            float x1,y1;
            x=(num1+pow((num1*num1-4*num2),0.5))/2.0;
            x1=(num1+pow((num1*num1-4*num2),0.5))/2.0;//看看浮点数和整数是否一样。
            if(x==x1)
            {
                cout<<"Yes"<<endl;
            }
            else
            {
                cout<<"No"<<endl;
            }
    
        }
        return 0;
    }
  • 相关阅读:
    Java.io.outputstream.PrintStream:打印流
    Codeforces 732F. Tourist Reform (Tarjan缩点)
    退役了
    POJ 3281 Dining (最大流)
    Light oj 1233
    Light oj 1125
    HDU 5521 Meeting (最短路)
    Light oj 1095
    Light oj 1044
    HDU 3549 Flow Problem (dinic模版 && isap模版)
  • 原文地址:https://www.cnblogs.com/William-xh/p/6957179.html
Copyright © 2011-2022 走看看