当前位置:网站首页>Niu Ke's question -- finding the least common multiple
Niu Ke's question -- finding the least common multiple
2022-06-11 18:30:00 【HHYX.】
Find the least common multiple
Topic link : Find the least common multiple
Title Description
Positive integer A And positive integers B The least common multiple of is Can be A and B The smallest positive integer value of an integer division , Design an algorithm , Find input A and B The least common multiple of .
Data range :1≤a,b≤100000
Input description :
Enter two positive integers A and B.
Output description :
Output A and B The least common multiple of .
Topic analysis
The least common multiple means that both can be A Divisibility can also be B The smallest positive integer of an integer . There are several solutions as follows :
1. brute-force attack
Start with the larger of the two numbers , Judge one by one , The first one you encounter can be A and B Divisible is the least common multiple , But the time complexity will be very high , Constant judgment is required .
2. Better solution
The least common multiple of two numbers is actually the product of two numbers divided by the least common divisor . The least common divisor can be obtained by rolling division . The rolling phase division method is realized as follows :
Here we use the second method to solve this problem , The code implementation is as follows :
Code implementation
#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;// assignment a,b
int tmp = gcd(a ,b);// The least common divisor
int ret=(a*b)/tmp;// The least common multiple is the multiplication and division of two numbers by the least common multiple
cout<<ret<<endl;
return 0;
}

边栏推荐
- 网络和并发编程常见面试题
- PIL-Pillow图像处理【1】-安装与新建
- ACL 2022:评估单词多义性不再困扰?一种新的基准“DIBIMT”
- 神经网络与深度学习-2- 机器学习简单示例-PyTorch
- System learning typescript (V) - joint type
- 5 minutes to understand the red, blue and purple in the attack and defense drill
- [C语言]用结构体按分数高低降序输出学生的姓名和分数
- [Golang]力扣Leetcode - 292. Nim 游戏(数学)
- 学习使用LSTM和IMDB评论数据进行情感分析训练
- 5分钟了解攻防演练中的红蓝紫
猜你喜欢

Sorted circular linked list

力扣刷题——二叉树的最近公共祖先

牛客刷题——求最小公倍数

全志科技T3開發板(4核ARM Cortex-A7)——MQTT通信協議案例

Non recursive traversal of binary tree

使用Visdom对损失函数进行监控

牛客刷题——两种排序方法

TR-069 protocol introduction

Cryptology Summary
![[c language] output the average score and the student data below or equal to the average score with the structure](/img/c4/263301a22b61c86a3e0df6ad2596f1.png)
[c language] output the average score and the student data below or equal to the average score with the structure
随机推荐
SISO decoder for min sum (supplementary Chapter 2)
Sa-Token 单点登录 SSO模式二 URL重定向传播会话示例
HashSet集合
为何TI的GPMC并口,更常被用于连接FPGA、ADC?我给出3个理由
Monitoring loss functions using visdom
力扣33题,搜索旋转排序数组
[C语言]用结构体按分数高低降序输出学生的姓名和分数
Getting started with CTF
SISO decoder for a general (n, n-1) SPC code (supplementary Chapter 3)
. Net core redis hyperloglog type
Some thoughts on how to do a good job of operation and maintenance management
JS实现全屏展示的具体方法
[golang] leetcode - 292 Nim games (Mathematics)
Ubuntu installs PSQL and runs related commands
Force deduction 32 questions longest valid bracket
牛客刷题——part6
论工作流选型
金融银行_催收系统简介
论催收系统的管理子系统选型设计
Common interview questions of network and concurrent programming