求两个数的最小公倍数,两个数的最小公倍数为:能被这两个数同时整除的最小的数。
#include<iostream>
using namespace std;
//求最大公约数 使用辗除法
int gcd(int a,int b)
{
int tmp;
while(b)
{
tmp = a%b;
a = b;
b =tmp;
}
return a;
}
int main()
{
//最小公倍数=两个数的乘积/最大公约数
int a,b;
cin>>a>>b;
cout << a/gcd(a,b)*b <<endl; //注意:小心x*y直接溢出大于最大范围.
return 0;
} 或者: #include<iostream>
using namespace std;
//求最大公约数 相减法
int gcd(int a,int b)
{
while(a != b)
{
if( a > b)
a = a - b;
else
b = b - a;
}
return a;
}
int main()
{
//最小公倍数=两个数的乘积/最大公约数
int a,b;
cin>>a>>b;
cout << a/gcd(a,b)*b <<endl; //注意:小心x*y直接溢出大于最大范围.
return 0;
} int main()
{
int a , b ;
scanf("%d %d",&a,&b);
int i = 1;
while ((a * i) % b != 0)
{
i++;
}
printf("%d\n", i * a);
return 0;}
Scanner in = new Scanner(System.in);
int a=in.nextInt();
int b=in.nextInt();
if(a>b){
int tmp=a;
a=b;
b=tmp;
}
ArrayList<Integer> ar=new ArrayList<>();
for(int i=a;i>1;i--){
if(a%i==0&&b%i==0){
a/=i;
b/=i;
ar.add(i);
}
}
int res=a*b;
for(int i=0;i<ar.size();i++){
res*=ar.get(i);
}
System.out.println(res); 今天华泰证券笔试,我这道题这么写的值过了83%大佬们帮看看哪里有问题#include <stdio.h>
int main() {
long long int a, b;
scanf("%lld %lld", &a, &b);
long long int tmp = a*b;
//确保 a 大于 b
if (b > a)
{
a = a^b;
b = a^b;
a = a^b;
}
while(b != 0)
{
a = a%b;
if (a == 0) //a 等于 b 的情况
{
a = b;
break;
}
b = b%a;
}
a = tmp /a;
printf("%lld", a);
return 0;
} #include <iostream>
using namespace std;
int GCD(int a,int b)
{
int temp=0;
while(b)//辗转相除法求最大公约数
{
temp=a%b;
a=b;
b=temp;
}
return a; //a为最终的最大公约数
}
int main()
{
int m,n;
int gcd,ret;
while(cin>>m>>n)
{
gcd=GCD(m,n);
ret=m/gcd*n; //此处的m*n可能会溢出
cout<<ret<<endl;
}
return 0;
}
//此方法可能复杂度过大运行时间长
//#include <iostream>
//using namespace std;
//int main()
//{
// int A,B,temp;
// while(cin>>A>>B)//循环输入
// {
// for((A>B)? temp=A :temp=B; ;temp++)//找能被AB同时整除的/.最小正整数temp
// {
// if((temp%A==0) && (temp%B==0))
// break;
// }
// cout<<temp<<endl;//输出最小公倍数
// }
// return 0;
//} #include<iostream>
#include <utility>
using namespace std;
int GCB(int a, int b)
{
if(a < b)
swap(a,b);
int i = b;
for(; b > 0; --i)
{
if(a%i == 0 && b%i == 0)
break;
}
return i;
}
int main()
{
int A,B;
cin >> A >> B;
cout << ((A/GCB(A,B)) * (B/GCB(A,B))* GCB(A,B)) << endl;
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;
} #include <utility>
template<typename NumType>
NumType gcd(NumType a, NumType b) {
NumType k = 0;
while (b != 0) {
if (b % 2 == 0) {
if (a % 2 == 0) {
k++;
a /= 2; b /= 2;
}
else {
b /= 2;
}
}
else {
if (a % 2 == 0) {
a /= 2;
}
else {
if (a < b) {
std::swap(a, b);
}
a -= b;
}
}
std::swap(a, b);
b %= a;
}
return a<<k; // 等价于a*pow(2,k)
}
template<typename NumType>
NumType lcm(NumType a, NumType b) {
return a/gcd(a,b)*b;
}