Loading...

BISHI57 最大公因数与最小公倍数

在这里插入图片描述

###思路 在这里插入图片描述

求解代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));

        String[] str = br.readLine().trim().split("\\s+");
        Long a = Long.parseLong(str[0]);
        Long b = Long.parseLong(str[1]);

        long g = gcd(a,b);

        long l = (a==0||b==0)?0:(a/g)*b;

        out.println(g+" "+l);
        out.flush();
        out.close();
        br.close();
    }

    private static long gcd(long a, long b) {

        while (b != 0) {
            long tmp = b;
            b = a%b;
            a=tmp;
        }
        return a;
    }
最后更新于 2026-04-05 17:35:33
Code Road Record