UVA 11889 Benefit——gcd

来源:互联网 发布:cmm软件内涵 编辑:程序博客网 时间:2024/05/19 02:26

步骤:

1若c % a != 0 , 输出no, 否则令b = c / a

2另g=gcd(a, b);

3如果g不为1,则a = a / g, b = b * g, 然后重复2直到g为1

4输出b

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;int gcd(int x, int y) { return (y == 0) ? x : gcd(y, x % y); }int main() {    int T; scanf("%d", &T);    for (int kase = 1; kase <= T; kase++) {        int a, c;        scanf("%d %d", &a, &c);        if (c % a) {            printf("NO SOLUTION\n"); continue;        }        int b = c / a;        int g = gcd(a, b);        while (g != 1) {            a /= g;            b *= g;            g = gcd(a, b);        }        printf("%d\n", b);    }    return 0;}


原创粉丝点击