查找两个数字,其总和和GCD在C ++中给出
我们有两个数字a和b的总和和gcd。我们必须找到数字a和b。如果不可能,则返回-1。假设总和为6,gcd为2,则数字为4和2。
这种方法就像给出GCD一样,然后知道数字将是GCD的倍数。现在执行以下步骤
如果我们选择第一个数字作为GCD,则第二个数字将为和-GCD
如果在上一步中选择的数字总和与总和相同,则打印两个数字。
否则打印-1,因为不存在数字。
示例
#include <iostream>
#include <algorithm>
using namespace std;
void printTwoNumbers(int s, int g) {
if (__gcd(g, s - g) == g && s != g)
cout << "first number = " << min(g, s - g) << "\nsecond number = " << s - min(g, s - g) << endl;
else
cout << -1 << endl;
}
int main() {
int sum = 6;
int gcd = 2;
printTwoNumbers(sum, gcd);
}输出结果
first number = 2 second number = 4
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下