Sicily 1793. Numbers

本文探讨了如何通过给定的最大公约数(GCD)和最小公倍数(LCM),找到两个整数a和b之间的最小差异。详细介绍了算法实现过程,并提供了样例输入和输出,帮助理解解题策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值