当前位置:网站首页>牛客刷题——求最小公倍数
牛客刷题——求最小公倍数
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;
}

边栏推荐
- 高并发架构设计
- SISO decoder for a general (n,n-1) SPC code(补充章节3)
- 关于keil中,while循环条件不成立却无法跳出的问题
- 剑指 Offer(第 2 版)
- Hwang
- EditText 金额限制
- [c language] limit the number of searches and output the maximum value found in the number of internal searches
- NFT platform development NFT mall source code NFT mall development chain game development
- Several commands related to domain name
- [c language] shift elements after sorting elements of an array
猜你喜欢

labelme进行图片数据标注

SISO decoder for a general (n,n-1) SPC code(補充章節3)

Several commands related to domain name

求字符串中最大的 3 位相同数字
![[C语言]压缩字符串并添加标记字符](/img/b7/f7918f3ee0c409faffc70addd5ee65.png)
[C语言]压缩字符串并添加标记字符

Use egg Js+mongodb simple implementation of curdapi

使用Visdom對損失函數進行監控

Cryptology Summary

SISO decoder for min sum (supplementary Chapter 2)
![[c language] output students' names and scores in descending order of scores with structures](/img/41/b9dba88941560b296f4d7153b7c684.png)
[c language] output students' names and scores in descending order of scores with structures
随机推荐
[c language] output the average score and the student data below or equal to the average score with the structure
论高可用架构
10 ways to reset any user password
[c language] output the students within the specified score range with the structure
Radio button text background changes at the same time
Network Security Threat Intelligence System
Experiment 2: write a program and verify that the linear table sequence represents all operations
[c language] output the students with the highest scores with a structure. There can be multiple highest scores
最长严格递增子序列
判断是否为平衡二叉树
【C】 ATOI function implementation +offsetof implementation + exchange binary odd and even digits
Easycwmp source code analysis
ACL 2022: is it no longer difficult to evaluate word polysemy? A new benchmark "dibimt"
v-for循环遍历
Introduction to social engineering practice
Various poses for text modification using sed
LeetCode_ Prefix tree_ Medium_ 208. implement trie (prefix tree)
软件测试技术复习
On the problem that the while loop condition in keil does not hold but cannot jump out
Radiogroup dynamically add RadioButton