当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
随机推荐
In the process of quantitative trading, retail investors can do this
The difference between show and exec in Qt dialog
strlen(), strcpy(), strncpy(), strcat(), strncat(), strcmp(), strncmp()函数的封装
bin文档读写
【超级账本开发者系列】专访——肖慧 : 不忘初心,方得始终
如何将List<Map>进行分组数值计算合并排序
Qt control - QTextEdit usage record
How to flexibly use the advantages of the quantitative trading interface to complement each other?
注解与反射
Use tensorboard remotely on the server
写在光学之前--振动和波
Several important functional operations of general two-way circular list
How can I know if quantitative programmatic trading is effective?
ImageWatch无法显示图像
怎么用VS+Qt创建新项目
复数与复数域
What do professional quantitative traders think about quantitative trading?
bin document read and write
浅谈ArraryList的浅克隆和深克隆
程序化交易规则对于整个交易系统有什么意义?









