当前位置:网站首页>牛客刷题——求最小公倍数
牛客刷题——求最小公倍数
2022-06-11 18:14:00 【HHYX.】
题目链接:求最小公倍数
题目描述
正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
数据范围:1≤a,b≤100000
输入描述:
输入两个正整数A和B。
输出描述:
输出A和B的最小公倍数。
题目分析
最小公倍数指的是既能被A整除也能被B整数的最小的正整数。有如下几种解法:
1.暴力破解法
从两个数中较大的数开始,逐个向上进行判断,遇到的第一个能够被A和B整除的就是最小公倍数,但是这样时间复杂度会很高,需要不断进行判断。
2.更优解法
两个数的最小公倍数实际上就是两个数的乘积除以最小公约数。最小公约数可以使用辗转相除法进行求得。辗转相除法实现如下:
这里使用第二种方法来解决该问题,代码实现如下:
代码实现
#include<iostream>
using namespace std;
int gcd(int m,int n)
{
int ret=0;
while(ret=m%n)
{
m = n;
n = ret;
}
return n;
}
int main()
{
int a,b;
cin>>a>>b;//赋值a,b
int tmp = gcd(a ,b);//最小公约数
int ret=(a*b)/tmp;//最小公倍数为两个数相乘除以最小公倍数
cout<<ret<<endl;
return 0;
}

边栏推荐
- Codeworks round 481 (Div. 3) [done]
- The HashSet collection stores student objects and traverses
- 排序的循环链表
- 新项目 搭建环境方法
- 【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
- 10 ways to reset any user password
- LDPC 7 - 解码简单例子
- On the problem that the while loop condition in keil does not hold but cannot jump out
- Say no to credit card fraud! 100 lines of code to realize simplified real-time fraud detection
- 剑指 Offer(第 2 版)
猜你喜欢

合并多棵二叉搜索树
![[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration](/img/ae/9a0c300f2dcb03b05d737f14b0955f.jpg)
[not forgetting the original intention and forging ahead] 2021 Zhongchuang Suanli new year conference and anniversary celebration

Some problems of DC-DC bootstrap capacitor

v-for循环遍历
![Spring 2021 daily question [week3 not finished]](/img/a4/72f2235d014613d26be0fc3524d236.jpg)
Spring 2021 daily question [week3 not finished]
![Codeworks round 479 (Div. 3) [done]](/img/a0/f3c6989d8f755c03076b237514ee64.jpg)
Codeworks round 479 (Div. 3) [done]

10 ways to reset any user password

EasyCwmp源码分析
![Acwing game 40 [End]](/img/6b/a9ea5e1f9578d21219c69571e8b0f8.jpg)
Acwing game 40 [End]

Oracle高级数据库复习
随机推荐
vim常用命令
TR-069 protocol introduction
Spring 2021 daily question [end of week4]
[c language] limit the number of searches and output the maximum value found in the number of internal searches
SISO decoder for min sum (supplementary Chapter 2)
NR LDPC 打孔-punctured
Common interview questions of network and concurrent programming
[C语言]限制查找次数,输出次数内查找到的最大值
使用Visdom對損失函數進行監控
Class question: how to ensure that line table storage can be inserted at any time?
Radio button text background changes at the same time
力扣32题最长有效括号
Common operations of Visio
ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
Experiment 3: design and verify all operations represented by linear table sequence on the computer
排序的循环链表
SQL报错注入1
File class learning
Acwing game 40 [End]
HashSet collection