hdu 5974 A Simple Math Problem 一元二次方程

已经是第三遍做这个题了 

(两个数的和)和(这两个数的最小公倍数)的最大公约数等于这两个数的最大公约数  gcd(x,y)=gcd(x+y,lcm(x,y))

设x是其中一个数 另一个数为a-x  

x*(a-x)/gcd(a,b)=lcm(a,b)

#include <iostream>
#include <stdio.h>
#include<string.h>
#include<math.h>
#include <algorithm>
#define pi 3.1415926
using namespace std;
int gcd (int m,int n)
{
    if(n==0)
        return m;
    else
        return gcd(n,m%n);
}
int main()
{
   int a,b;
    while(cin>>a>>b)
    {

       int s=gcd(a,b);
       int ans1,ans2;
       double aa=a*a-4*b*s;
       if((a*a-4*b*s)<0)
        puts("No Solution");
       else
       {
           if(sqrt(aa)==(int)sqrt(aa)&&(a-(int)sqrt(aa))%2==0&&(a+(int)sqrt(aa))%2==0)
           {
              ans1=(a-sqrt(aa))*0.5;
                ans2=(a+sqrt(aa))*0.5;
                printf("%d %d\n",ans1,ans2);
           }
           else
             puts("No Solution");
       }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/yangdelu855/article/details/79047377