您的位置:首页 > 其它

UVA 11889 Benefit——gcd

2017-11-19 11:27 190 查看
步骤:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: