dut-最大公约数和最小公倍数

阅读 145

2022-03-19

输入格式:

输入有若干组。

每组数据,在一行中给出两个正整数M和N(≤1000),中间有1个空格。

输出格式:

对于每组输入,在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1个空格分隔。

给定2个正整数,求它们的最大公约数和最小公倍数,并输出。

输入样例:

18 12
20 15
39 26
5 76
45 25
1993 343

输出样例:

在这里给出相应的输出。例如:

6 36
5 60
13 78
1 380
5 225
1 683599
import java.util.Scanner;
public class Main{
    public static void main(String[] arys){
        int i=0;
        int j=0;
        int k=0;
        int l=1;
        int a=0;
        int b=0;
        int c=0;
        Scanner sc=new Scanner(System.in);
        while(sc.hasNextInt()){
        i=sc.nextInt();
        j=sc.nextInt();
            a=i;
            b=j;
            //求最大公因数
        while(l!=0){
            if(j>i){
                k=i;
                i=j;
                j=k;
            };
            l=i%j;
            if(l==0){
                System.out.print(j+" ");
            }else{
                i=l;
            };
        };
                                    c=a*b/j;
            System.out.println(c);
            l=1;
        };
    }
}

精彩评论(0)

0 0 举报