正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
数据范围:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(), b = in.nextInt(); System.out.println(a*b/gcd(a,b)); } // 辗转相除法求最大公因数 public static int gcd(int a, int b) { if (a % b == 0) return b; return gcd(b, a % b); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int num1 = scanner.nextInt(); int num2 = scanner.nextInt(); int result = 1; // i <= Math.min(num1, num2)防止两数成倍数关系 for (int i = 2; i <= Math.min(num1, num2); i++) { if (num1 % i == 0 && num2 % i == 0) { // 先把公约数进行乘积运算 result *= i; num1 /= i; num2 /= i; i--; } } //将所有公约数乘积后再对没有除尽的进行乘积运算 result *= num1 * num2; System.out.println(result); } scanner.close(); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m = sc.nextInt(); int n = sc.nextInt(); int bottom = Math.max(m, n); while (true) { if (bottom % m == 0 && bottom % n == 0) { break; } bottom++; } System.out.println(bottom); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int num1 = in.nextInt(); int num2 = in.nextInt(); for(int i = num1 > num2 ? num1 : num2; i <= num1 * num2; i++){ if(i % num1 == 0 && i % num2 == 0){ System.out.println(i); break; } } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int a = scanner.nextInt(); int b = scanner.nextInt(); int result = 1; // 所有公约数相乘 for (int i = 2;i<=Math.min(a,b);i++) { while (a%i==0 && b%i==0){ result*=i; a = a/i; b = b/i; } } result = result*a*b; System.out.println(result); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int A = in.nextInt(); int B = in.nextInt(); for (int i = Math.min(A,B); i <= A*B; i++){ if ((i%A==0) && (i%B==0)){ System.out.println(i); break; } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(), b = in.nextInt(); System.out.println((a * b) / gcd(a, b)); } public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); // 最小公倍数 = a*b/(ab的最大公约数) if (a < b) { int temp = a; a = b; b = temp; } System.out.println(a * b / zdgys(a, b)); } public static int zdgys(int a, int b) { int c = a % b; if (c == 0) return b; return zdgys(b, c); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextInt()) { int a = in.nextInt(); int b = in.nextInt(); int max = Math.max(a, b); int min = Math.min(a, b); for(int i=1; i<=min; i++){ if((max*i)%min==0){ System.out.println(max*i); break; } } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); int b = in.nextInt(); for(int i = 1;i <= a*b;i++){ if(i % a ==0 && i % b == 0){ System.out.println(i); break; } } } }
import java.util.Scanner; // 辗转相除法 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); int c = 1;//用来存储最大公约数 for (int i = 2; i <= a; i++) { if (b % i == 0 && a % i == 0) { c = c * i; b = b / i; a = a / i; i--; } } //最终结果等于最大公约数乘各自的剩余互质的数 System.out.println(c * a * b); } }