标签:space tip 最小公倍数 ase pac iostream class cas one
Given two positive integers a and b,find suitable X and Y to meet the conditions:
X+Y=a
Least Common Multiple (X, Y) =b
#include <iostream> #include <cmath> using namespace std; int X , Y; int gcd(int a , int b) { return b > 0 ? gcd(b , a % b) : a; } int cal(int a , int b , int c) { if(a * a - 4 * b < 0) return 0; int gg = a * a - 4 * b; int fff = sqrt(gg); if(gg != fff * fff) { Y = -1 , X = -1; return 0; } X = (a + fff); Y = (a - fff); if(X % 2 == 0 && Y % 2 == 0) { X /= 2; Y /= 2; return 1; } else { X = -1; Y = -1; return 0; } } int main() { int a , b; while(cin >> a >> b) { int c = gcd(a , b); X = -1 , Y = -1; if(cal(a / c , b / c , c) && X != -1 && Y != -1) { cout << min(X , Y) * c << ‘ ‘ << max(X , Y) * c << endl; } else { cout << "No Solution" << endl; } } return 0; }
hdu 5974 A Simple Math Problem(数学题)
标签:space tip 最小公倍数 ase pac iostream class cas one
原文地址:http://www.cnblogs.com/TnT2333333/p/6048463.html