while True: try: a,b = map(int,input().split()) if a%b==0: #先讨论b是a的因子的情况 print(a) elif b%a==0: #a是b的因子的情况 print(b) else: factor=1 for i in range(max(a,b),1,-1): #从a和b中较大的一个开始试因子,直到2,倒叙 if a%i==0 and b%i==0: factor *= i #公因子累积 a=a/i #a和b分别除以公因子 b=b/i else: continue result=int(a*b*factor) #除以所以公因子后的a和b,乘所有公因子之积 print(result) except: break
import java.util.*;
public class Main{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int a=sc.nextInt();
int b=sc.nextInt();
int max=a>b?a:b;
int x=max;
for(;;x+=max){
if(x%a==0 && x%b==0){
System.out.println(x);
return;
}
}
}
}
} import java.util.Scanner;
//暴力法,时间复杂度高
// public class Main{
// public static void main(String[] args){
// Scanner sc = new Scanner(System.in);
// while(sc.hasNext()){
// int a = sc.nextInt();
// int b = sc.nextInt();
// int res = 0;
// if(a>b) res=fun(b, a);
// else if(a<b) res=fun(a, b);
// else res=a;
// System.out.println(res);
// }
// }
// private static int fun(int a, int b){
// for(int i=b; i<=a*b; i++){
// if(i%a==0 && i%b==0) return i;
// }
// return 0;
// }
// }
//辗转相除法求最大公约数gbc(a,b)= gbc(b, a%b);,最小公倍数=两数乘积 / 最大公约数
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println((a*b)/gcb2(a, b));
}
}
//递归写法gbc(a,b)= gbc(b, a%b)
private static int gcb1(int a, int b){
if(b==0) return a;
//后面递归b大,a%b小,所以b先达到0
return gcb1(b, a%b);
}
//迭代写法
private static int gcb2(int a, int b){
while(b>0){
int temp = a%b;
a = b;
b = temp;
}
return a;
}
} #include<stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a,&b);
for(int i=a;i<=a*b;i++)
{
if(i%a==0 && i%b ==0)
{
printf("%d",i);
break;
}
}
return 0;
} /**
Swift 没有使用递归调用的版本
思路:老老实实按照语文老师教的写了个 栈,把每次的公约数压了进去,最后除不尽时再将两位数字一起压入。解法可用于求最小公约数~
*/
while let value = readLine() {
print("\(solution(value))")
}
func solution(_ inputStr: String) -> Int {
let numArray = inputStr.split(separator: " ")
var numA = Int(numArray[0]) ?? 0
var numB = Int(numArray[1]) ?? 0
var numStack = [Int]()
var count = 2
while count <= max(numA, numB) {
if numA % count == 0, numB % count == 0 {
numStack.append(count)
numA /= count
numB /= count
// 达到最小值情况提前退出
if count >= min(numA, numB) {
numStack.append(numA)
numStack.append(numB)
break
}
continue
} else if numA % count == 0 {
numStack.append(count)
numA /= count
} else if numB % count == 0 {
numStack.append(count)
numB /= count
}
if count >= min(numA, numB) {
numStack.append(numA)
numStack.append(numB)
break
}
count += 1
}
return numStack.reduce(1) { $0 * $1 }
} def gcd(a, b): """Return greatest common divisor using Euclid's Algorithm.""" while (a % b != 0): t = a % b a = b # 新的a用原来的b值 b = t # 新的b用原来的余数值 return b # 若a除以b能整除,则b就是两个数的最大公约数。 # 首先,从键盘键入两个数a和b的值,变量t来保存余数。用while循环来判断能否整除,根据“辗转相除法”,先用第一个数a÷b再将除数b赋给a, # 余数赋给b,循环往复,直到能整除时结束循环,此时的除数b即为最大公约数。 def lcm(a, b): """Return lowest common multiple.""" return a * b // gcd(a, b) while True: try: a, b = map(int, input().split()) print(lcm(a, b)) except: break
特别说明:若a<b,例如a=18,b=99。t=a%b=18%99=18;新的a用原来的b值————a=99;新的b用原来的余数值b=t=18。我们发现通过一次循环交换了a、b的值,这时就能满足a>b的条件了,再继续根据辗转相除的方法即可得到最大公约数。因此,无须顾及输入的两个整数a和b谁大谁小。
利用欧几里得公式进行计算;
import java.util.Scanner;
/**
* @author : cunyu
* @version : 1.0
* @className : OneZeroEight
* @date : 2020/8/8 22:41
* @description : 108.求最小公倍数
*/
public class OneZeroEight {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (input.hasNext()) {
int m = Integer.parseInt(input.nextLine().split(" ")[0]);
int n = Integer.parseInt(input.nextLine().split(" ")[1]);
System.out.println(getLcm(m, n));
}
}
/**
* @param m
* @param n
* @return
* @description 求最大公约数
* @date 2020/8/8 22:50
* @author cunyu1943
* @version 1.0
*/
public static int getGcd(int m, int n) {
while (n > 0) {
int tmp = m % n;
m = n;
n = tmp;
}
return m;
}
/**
* @param m
* @param n
* @return
* @description 求最小公倍数
* @date 2020/8/8 22:50
* @author cunyu1943
* @version 1.0
*/
public static int getLcm(int m, int n) {
return m * n / getGcd(m, n);
}
}
#include <iostream>
using namespace std;
int func(int a,int b)
{
int i = 0;
int m = 0;
while(1)
{
i++;
m = a*i;
if(m%b==0)
{
break;
}
}
return m;
}
int main()
{
int a,b,result;
scanf("%d %d",&a,&b);
result = func(a,b);
std::cout<<result;
return 0;
}
#include <iostream>
using namespace std;
#define min(x,y) ((x)<(y) ? (x):(y))
#define max(x,y) ((x)>(y) ? (x):(y))
int LowestCommonMultiply(int a, int b)
{
int small = min(a,b);
int big = max(a,b);
for (int i = 1; i < big;i++)
{
if((small*i)%b == 0)
{
return (small*i);
}
}
return a*b;
}
int main()
{
int ret;
int a,b;
cin>>a>>b;
ret = LowestCommonMultiply(a,b);
cout<<ret;
return 0;
} ''' python 代码求解 正解:先求公约数,再求公倍数 暴力:a,b的最小公倍数 must <=a*b ,所以只要遍历 2~a*b 即可 ''' a,b = map(int,input().split()) num = a if a>b else b for i in range(2,num+1): if (a%i==0) and (b%i==0): num = i break if num==(a if a>b else b): print(a*b) else: print(int(a*b/num))
#include<iostream>
using namespace std;
int max_gongyueshu(int A, int B)
{
int Mgongyueshu = 1;
for(int i = 1; i <= A && i <= B; i++)
{
if(A % i == 0 && B % i == 0)
{
Mgongyueshu = i;
}
}
return Mgongyueshu;
}
int main()
{
int A, B;
cin >> A >> B;
int max_gys = max_gongyueshu(A, B);
cout << (A*B)/max_gys << endl;
return 0;
}