正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
#include<bits/stdc++.h> using namespace std; int a,b; int main(){ cin>>a>>b; cout<<a*b/(__gcd(a,b)); return 0; }
#include <stdio.h> int min_gong(int a, int b) { int min = 0; int i = 1; while (1) { min = i * a; if (min % b == 0) { return min; } i++; } } int main() { int a = 0; int b = 0; scanf("%d %d", &a, &b); printf("%d", min_gong(a, b)); return 0; }
#include <iostream> using namespace std; int gcb(int a, int b){ int r; while(r = a % b){ a = b; b = r; } return b; } int main(){ int a, b; cin >> a >> b; // 最小公倍数 = 两数之积除以最大公约数 // 这里使用碾转相除法进行最大公约数的求解: // 即a与b的最大公约数可以转化为a、b之间的余数为两者之间最小的数之间的公约数。 // 所以对于输入的两个数进行连续求余,直到余数为0,求余的分母即为结果。 cout << a * b / gcb(a, b); return 0; }
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)); } }
#include <stdio.h> #define MAX 100000000 int main() { int a, b; while (scanf("%d %d", &a, &b) != EOF) { // 注意 while 处理多个 case int max; if(a>=b){ max = a; }else{ max = b; } if(max%a==0 && max%b==0){ printf("%d",max); }else{ for(int i = max; i<MAX; i++){ if(i%a==0 && i%b==0){ printf("%d",i); break; } } } // 64 位输出请用 printf("%lld") to // printf("%d\n", a + b); } return 0; }
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); } }
#define CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<math.h> void asr(int a, int b) { int i = a>b?b:a; int j = sqrt(i); int k = 0; for(k = j; k > 0; k--) { if( a%k == 0 && b%k ==0) { printf("%d", a*b/k); break; } } } int main() { int a = 0, b = 0; scanf("%d %d", &a, &b); asr(a, b); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int a = 0; int b = 0; scanf("%d%d", &a, &b); int max = a > b ? a : b; //先找出两个数中的最大值,并赋值给max while (1) { if ((max%a == 0) && (max%b == 0)) //判断max是否可以整除两数 { printf("%d\n", max); break; } else max++; //若max不可整除两数,则令max++,最终经过有限次循环一定可以找到可以同时整除两数的最小数,且此时该数为两数的最小公倍数 } return 0; }
/*************************************************** * Author : cornfieldchase * Blog : cornfieldchase2014.com * Filename : 求最小公倍数.cpp * Description : * Last modified : 2019-11-14 18:45 ***************************************************/ #define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> using namespace std; int main() { int m, n, i; cin >> m >> n; for (i = m;; i++) { if (i%m == 0 && i%n == 0) break; } cout << i; }
def gcd(a, b): if(b == 0): return a return gcd(b, a%b) def lcm(a, b): if(a < 1 or b < 1): return 0 if(a < b): a = a + b b = a - b a = a - b return (a*b / gcd(a, b)) a, b = input().split() c = lcm(int(a), int(b)) print(int(c))