题意:给你两个数G和L,输出两个正整数,最大公约数为G,最小公倍数为L,输出a最小的情况,如果不存在输出-1。
思路:当a最小时,a = G,所以只要L % G == 0,就表示存在。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int g, l;
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d", &g, &l);
if (g > l) {
swap(g, l);
}
if (l % g == 0)
printf("%d %d\n", g, l);
else
printf("-1\n");
}
return 0;
}
本文介绍了一个简单的算法问题,即根据给定的最大公约数G和最小公倍数L,找到符合条件的两个正整数a和b,使得a的值尽可能小。文章提供了一段C++代码实现,并解释了判断解的存在性的方法。
398

被折叠的 条评论
为什么被折叠?



