题解 | #游游的最小公倍数#

游游的最小公倍数

https://www.nowcoder.com/practice/385c7aa397e54bb58f36286ab0d65156

#include <stdint.h>
#include <stdio.h>

long long gcd(long long a, long long b) {
    int r = 1;
    while(r != 0) {
        r = b % a;
        b = a;
        a = r;
    }
    return b;
}

long long lcm(long long a, long long b) {
    return a * b / gcd(a, b);
}


int main() {
    int t;
    if (scanf("%d", &t) != EOF) {
        long long n;
        for(int i = 0; i < t; i++) {
            if(scanf("%lld", &n) != EOF) {
                long long a,b;

                for(a = n / 2, b = n - a; a > 0; a--, b++) {
                    // printf("a is %lld, b is %lld\n", a, b);
                    if(gcd(a,b) == 1) {
                        // printf("%lld\n",lcm(a,b));
                        break;
                    }
                }
                printf("%lld %lld\n", a, b);
            } else printf("error");
        }
    } else printf("error1");
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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