正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
import java.util.*; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int m = scan.nextInt(); int n = scan.nextInt(); int c = m * n / sameNum(m,n); System.out.println(c); } public static int sameNum(int a,int b) { int c = a % b; while(c != 0) { a = b; b = c; c = a % b; } return b; } }
import java.util.*; public class Main{ public static int gdc(int n, int m){ if(m == n){ System.out.println(n); } if(n < m){ int tmp = n; n = m; m = tmp; } int r = 0; //辗转相除法求最大公约数 while((r = n % m) > 0){ n = m; m = r; } return m; } public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int mn = gdc(n,m); System.out.println((n * m)/mn); } } //方法2 import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int a = Math.max(m,n); int b = Math.min(m,n); int c = 0; //辗转相除法求最大公约数 while((c = a % b) > 0){ a = b; b = c; } System.out.println((n * m)/b); } }
import java.util.*; public class Main{ public static int gcd (int a, int b) { if (b == 0){ return a; } return gcd (b,a%b); } public static void main (String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println((a*b)/gcd(a,b)); } }