题解 | #小乐乐与欧几里得#

小乐乐与欧几里得

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;
}
全部评论

相关推荐

宇算唯航:目测实缴资本不超100W的小公司
点赞 评论 收藏
分享
半解316:内容充实,细节需要修改一下。 1,整体压缩为一页。所有内容顶格。 2,项目描述删除,直接写个人工作量 修改完之后还需要建议,可以私聊
点赞 评论 收藏
分享
Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务