当前位置:网站首页>Find the greatest common divisor and the least common multiple (C language)
Find the greatest common divisor and the least common multiple (C language)
2022-07-07 10:34:00 【Pu Yu Mu Zhi】
List of articles
One 、 subject
Write two functions , Find the maximum common divisor and the minimum common multiple of two integers respectively , Call these two functions with the main function . Two integers are entered by the keyboard .
Two 、 Analysis and code
1. Ideas
- greatest common divisor : The largest of the common divisors of two or more integers . Calculate by rolling division .
- Minimum common multiple : Two or more integers share a multiple divided by 0 The smallest one outside . Because the product of two numbers is equal to the product of the greatest common divisor and the least common multiple of the two numbers , therefore , The least common multiple of two numbers is equal to the product of these two numbers divided by the greatest common divisor .
- Analysis methods : Define two functions respectively , And two variables a,b. The first function uses the rolling division method to find a,b Maximum common divisor of , hypothesis a>b, Make a/b Get the remainder r, then b As divisor ,r As divisor , Get the remainder … Until the remainder is 0. The second function uses the above formula to obtain the least common multiple of two numbers .
2. Code
The code is as follows :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int Greatest_Common_Divisor(int a,int b); // Function declaration
int Least_Common_Multiple(int a,int b,int m); // Function declaration
int a,b,m,x;
scanf("%d,%d",&a,&b);
m=Greatest_Common_Divisor(a,b);
x=Least_Common_Multiple(a,b,m);
printf("%d and %d The greatest common divisor of :%d\n",a,b,m);
printf("%d and %d The minimum common multiple of is :%d\n",a,b,x);
return 0;
}
int Greatest_Common_Divisor(int a,int b) // Find the greatest common divisor
{
int temp,r;
if(b>a) // Let the larger of the two numbers be the dividend
{
temp=a;
a=b;
b=temp;
}
while((r=a%b)!=0)
{
a=b; // Divisor b Become a divisor a
b=r; // Make the remainder r Become a divisor b
}
return b;
}
int Least_Common_Multiple(int a,int b,int m) // Find the least common multiple
{
return (a*b/m);
}
3. Running results
边栏推荐
- 【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer
- 1324: [example 6.6] integer interval
- BigDecimal value comparison
- [detailed explanation of Huawei machine test] tall and short people queue up
- 深入理解Apache Hudi异步索引机制
- 字符串格式化
- 宁愿把简单的问题说一百遍,也不把复杂的问题做一遍
- 成为优秀的TS体操高手 之 TS 类型体操前置知识储备
- Prototype and prototype chain
- IPv4 socket address structure
猜你喜欢
求最大公约数与最小公倍数(C语言)
使用 load_decathlon_datalist (MONAI)快速加载JSON数据
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
如何顺利通过下半年的高级系统架构设计师?
01 use function to approximate cosine function (15 points)
Review of the losers in the postgraduate entrance examination
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
String formatting
ThreadLocal is not enough
Socket通信原理和实践
随机推荐
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
P2788 数学1(math1)- 加减算式
Slurm资源管理与作业调度系统安装配置
[daiy5] jz77 print binary tree in zigzag order
leetcode-560:和为 K 的子数组
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
Remote meter reading, switching on and off operation command
关于easyflash v3.3使用过程的记录
Multisim -- software related skills
Some superficial understanding of word2vec
使用U2-Net深层网络实现——证件照生成程序
A small problem of bit field and symbol expansion
Installation and configuration of slurm resource management and job scheduling system
leetcode-304:二维区域和检索 - 矩阵不可变
Kotlin实现微信界面切换(Fragment练习)
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
Multithreaded asynchronous orchestration
BigDecimal数值比较
根据设备信息进行页面跳转至移动端页面或者PC端页面
想考中级软考,一般需要多少复习时间?