HDU 2504 JAVA

来源:互联网 发布:开户银行数据 编辑:程序博客网 时间:2024/05/24 15:40

Problem Description
有三个正整数a,b,c(0

import java.util.Scanner;public class Main{    /**解题思路:      *由于a和c的最大公约数是b,所以a = xb,b=yb。现在相当于已知x,求y。      *我们由上面可知x和y一定是互质的。只要枚举一下最小的与x互质的自然数就是y了,但是题目要求不等于b,所以要加上不相等的条件。     * @author 胡龙华     */    public static void main(String[] args) {        Scanner sc  = new Scanner(System.in);        int t = sc.nextInt();        while(t-->0){            int a = sc.nextInt();            int b = sc.nextInt();            int temp = a/b;            int i=2;            while(true){                if(GCD(temp,i)==1){                    System.out.println(i*b);                    break;                }                i++;            }        }    }    private static int GCD(int temp, int i) {        if(temp<i){            int x = temp;            temp = i;            i = x ;        }        int r = 1;        while(r!=0){            r = temp%i;            temp = i;            i = r;        }         return temp ;    }    /*private static int GCD(int temp, int i) {        return temp == 0 ? i : GCD(i % temp, temp );    }*/}
0 0