题解 | #小乐乐与欧几里得#
小乐乐与欧几里得
https://www.nowcoder.com/practice/da13e0cf321e4df9acd0fdf0a433cbb0
#include <stdio.h>
typedef long long int_64;
int_64 getGreatestComDiv(int_64 x, int_64 y);
int_64 getLeastComMul(int_64 x, int_64 y, int_64 (*pf)(int_64, int_64));
int main() {
// enter two numbers
int_64 a, b;
scanf("%lld %lld", &a, &b);
printf("%lld\n", getGreatestComDiv(a, b) + getLeastComMul(a, b,
&getGreatestComDiv));
return 0;
}
int_64 getGreatestComDiv(int_64 x, int_64 y) {
int_64 result = 1, i = 2, temp;
// To confirm the larger number, select y to be the larger one.
if (x > y) {
temp = x;
x = y;
y = temp;
}
// Check for the greatest common divisor by while-loop
while (i <= x) {
// To make sure i is a common divisor of both two numbers
if ((x % i == 0) && (y % i == 0)) {
x /= i; // Keep the value of x to next loop
y /= i; // Keep the value of y to next loop
result *= i;
i = 2;
} else {
i++;
}
}
return result;
}
int_64 getLeastComMul(int_64 x, int_64 y, int_64 (*pf)(int_64, int_64)) {
int_64 result;
result = (x * y) / pf(x, y);
return result;
}