gcd会,那这道呢。。。
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17026 Accepted Submission(s): 7112
Problem Description
有三个正整数a,b,c(0 < a,b,c < 10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。
Input
第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。
Output
输出对应的c,每组测试数据占一行。
Sample Input 2 6 2 12 4
Sample Output 4 8
本题好暴力,只需要遍历,因为c!=b,而且c一定为k*b,所以c可以从2*b开始取。到 3*b, 4*b, 5*b,……直到找到-。-
哈,看代码
#include<stdio.h> int gcd(int a, int b) { return !b ? a : gcd(b, a%b); } int main() { int n, a, b, c; while (scanf("%d", &n) == 1) { while (n--) { scanf("%d%d", &a, &b); c = 2 * b; while (gcd(a, c) != b) c += b; printf("%d\n", c); } } return 0; }