public class Main{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scan = new Scanner(System.in); int n,m; while(scan.hasNext()){ n = scan.nextInt(); m = scan.nextInt(); System.out.println(lcm(n,m)); } scan.close(); } //求最大公约数,辗转相除法 static int gcd(int n,int m){ if(n<=0||m<=0) return 0; int a = Integer.max(n, m); int b = Integer.min(n, m); while(b!=0){ int temp = a; a = b; b = temp%b; } return a; } //最小公倍数*最大公约数=n*m static int lcm(int n,int m){ int gcd = gcd(n,m); int product = n*m; return product/gcd; } }
时间: 2024-10-10 03:40:23