当前位置:网站首页>At the beginning of the C language order 】 【 o least common multiple of three methods
At the beginning of the C language order 】 【 o least common multiple of three methods
2022-08-09 16:18:00 【Little busy week in _】
一、循环求解
- First two LCM must be greater than the number of the larger number of,And less than the product of two Numbers
- 找出a与b之间的较大值max
- 利用循环,令i=max,不断++,当i能同时整除a与b时,输出i,跳出循环
- 即i为最小公倍数
代码如下:
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
int max = a > b ? a : b; //找出较大值
int i = 0;
for (i = max; i <= a*b; i++) {
if (i%a == 0 && i%b == 0) {
printf("%d\n", i);
break;//跳出循环
}
}
return 0;
}
二、辗转相除法
以除数和余数反复做除法运算,当余数为 0 时,取当前算式除数为最大公约数
两数乘积=最小公倍数*最大公约数
By division algorithm to find the greatest common divisor that is the least common multiple
图解举例:

代码如下:
#include<stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
int m = a * b;
int r = 0;
while (r = a % b) {
//r为0时,跳出循环,此时b为最大公因数
a = b;
b = r;
}
printf("%d\n", m/b);//Product divided by the greatest common factor is equal to the least common multiple
return 0;
}
三、找最小 i 值
- There must be an integerk能够使k/a=i,k/b=j;
- a * iFor integerk为a的倍数,k%b为0表示k为b的倍数,即(a * i)%b==0,此时a和bCommon multiple ofk,即a * i;
- Minimum required LCM, find out to meet the requirements of i 值即可.
代码如下:
#include<stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
int i = 1;//i从1开始找
while (a*i % b) {
//(a*i)%b==0时为假,结束循环
i++;
}
printf("%d\n", a*i);
return 0;
}
边栏推荐
- Bessel function
- strlen(), strcpy(), strncpy(), strcat(), strncat(), strcmp(), strncmp()函数的封装
- docker安装nacos并且指定容器数据卷,数据库连接等
- Entity Framework Core知识小结
- 如何保证电脑硬盘格式化后数据不能被恢复?
- 回收站一直显示未清空的图标问题
- How to flexibly use the advantages of the quantitative trading interface to complement each other?
- 正则化原理的简单分析(L1/L2正则化)
- LNK1123: Failed during transition to COFF: invalid or corrupt file
- What are the hot topics in quantitative programmatic trading?
猜你喜欢
随机推荐
Welcome to use CSDN - markdown editor
突然想分析下房贷利率及利息计算
6大论坛,30+技术干货议题,2022首届阿里巴巴开源开放周来了!
贝塞尔函数
LNK1123:转换到COFF期间失败:文件无效或损坏
.Net Core动态注入
Grad CAM model visualization
常见编译问题
MySql中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
Bean的生命周期
What do professional quantitative traders think about quantitative trading?
It is deeply recognized that the compiler can cause differences in the compilation results
Matlab修改Consolas字体
如何让你的量化交易系统具有概率优势,具有正向收益预期呢?
【超级账本开发者系列】专访——肖慧 : 不忘初心,方得始终
异常学习笔记
流程控制学习
浅析Servlet三大容器的常用方法及其作用域
运算符学习
The difference between show and exec in Qt dialog









