最简方法:最大公约数与最小公倍数

xiaoxiao2021-02-27  351

public class MaximumCommonDivisor { // 最大公约数 public static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } // 最小公倍数 public static int gbd(int a, int b) { return a / gcd(a, b) * b; } public static void main(String[] args) { System.out.println("最小公倍数 :(17,12) = " + gbd(17, 12));// 204 System.out.println("最大公约数 :(17,12) = " + gcd(17, 12));// 1 System.out.println("最小公倍数 :(17,12) = " + gbd(18, 12));// 36 System.out.println("最大公约数 :(17,12) = " + gcd(18, 12));// 6 } }
转载请注明原文地址: https://www.6miu.com/read-4529.html

最新回复(0)