Time Limit: 1 secs, Memory Limit: 32 MB
Description
Given the greatest common divisor(GCD) and the least common multiple(LCM) of two integers a and b (a <= b), please find the minimum of b-a.
Input
Input may consist of several test data sets. For each data set, it can be format as below: There is just one line containing two integers, the GCD and the LCM, both are between [1, 10^9].
Input is ended by GCD = LCM = 0.
Output
For each data set, output one integer representing the minimum difference of a and b can be found, and then output a, then b, separated with only one space. If more than one pair of a and b can be found, output the one which has minimum a.
Sample Input
6 36
1 35
0 0
Sample Output
6 12 18
2 5 7
n(≧▽≦)n Just do it!
#include <iostream>
#include <cmath>
using namespace std;
int get_GCD(int num1, int num2)
{
if (num1 % num2 != 0)
{
return get_GCD(num2, num1 % num2);
}
else
return num2;
}
int main()
{
int GCD, LCM, a, b;
while (cin >> GCD >> LCM && (GCD || LCM))
{
暴力搜寻,从最接近的位置开始寻找
for (b = ceil(sqrtf(LCM * GCD)); b <= LCM; b++){
if (LCM * GCD % b == 0){
a = LCM * GCD / b;
if (get_GCD(a, b) == GCD){
break;
}
}
}
cout << b - a << " " << a << " " << b << endl;
}
return 0;
}