当前位置:网站首页>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;
}
边栏推荐
- What is a template engine?What are the common template engines?Introduction to common commands of thymeleaf.
- 函数调用约定
- 【C语言初阶】求最小公倍数的三种方法
- VS2010: devenv.sln solution save dialog appears
- [Mysql]--Transaction, transaction isolation level, dirty read, non-repeatable read, phantom read analysis
- How to achieve long-term benefits through the Tongdaxin quantitative trading interface?
- 二叉排序树的左旋与右旋
- JVM简学笔记
- .Net Core后台任务启停(BackgroundService)
- 运算符学习
猜你喜欢
随机推荐
ASP.Net Core实战——初识.NetCore
运算符学习
docker安装单机版redis、集群版redis
相干光(光学)
跨平台桌面应用 Electron 尝试(VS2019)
It is deeply recognized that the compiler can cause differences in the compilation results
贝塞尔函数
Suddenly want to analyze the mortgage interest rate and interest calculation
程序化交易规则对于整个交易系统有什么意义?
常见编译问题
编译器不同,模式不同,对结果的影响
几何光学简介
Sort method (Hill, Quick, Heap)
Simple analysis of regularization principle (L1 / L2 regularization)
navicat for Oraclel链接oracle 报错oracle library is not loaded的解决办法
通用的双向循环列表的几个比较重要的函数操作
.Net Core动态注入
OpenSSF's open source software risk assessment tool: Scorecards
【小白必看】初始C语言(上)
名词概念总结(不定期更新~~)









