正整数A和正整数B 的最小公倍数是指 能被A和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 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); } }
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(); in.close(); int lcm = A * B / gcd(A, B); System.out.println(lcm); } private static int gcd(int x, int y) { while (y != 0) { int temp = x % y; x = y; y = temp; } return x; } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num1 = in.nextInt(); int num2 = in.nextInt(); int num3 = num1 * num2; for (int i = 1; i <= num3; i++) { if (i % num1 == 0 && i % num2 == 0) { System.out.print(i); break; } } } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = in.nextLine().split(" "); Node node = new Node(Integer.parseInt(strings[0]), Integer.parseInt(strings[1])); int result = 1; while (!isDoublePrime(node)) { result *= node.num; node.x1 /= node.num; node.x2 /= node.num; } System.out.println(result * node.x1 * node.x2); } public static Boolean isDoublePrime(Node node) { for (int i = Math.min(node.x1, node.x2); i > 1; i--) if (node.x1 % i == 0 && node.x2 % i == 0) { node.num = i; return false; } return true; } } class Node { int x1; int x2; int num = 1; // 公因数 public Node(int x1, int x2) { this.x1 = x1; this.x2 = x2; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println(a/new Main().gcd(a,b)*b); } int gcd(int a, int b){ return b ==0?a:gcd(b,a%b); } }