又见GCD
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14717 Accepted Submission(s): 6161
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
Source
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int gcd(int x,int y){
int min,max;
int t;
min=x<y? x:y;
max=x>y? x:y;
while(min!=0){
t=min;
min=max%min;
max=t;
}
return max;
}
int main(){
int n;
int a,b,c,min,max;
scanf("%d",&n);
while(n--){
scanf("%d%d",&a,&b);
c=2*b;
while(b!=gcd(a,c)){
c=c+b;
}
printf("%d\n",c);
}
return 0;
}
/*
*/