当前位置:网站首页>函数:求两个正数的最大公约数和最小公倍
函数:求两个正数的最大公约数和最小公倍
2022-07-06 09:24:00 【|光|】
要求
写两个函数,分别求两个正数的最大公约数和最小公倍数,用主函数调用这两个函数并输出结果。两个正数由键盘输入。
代码
/* * 该函数用来计算两个数的最大公约数 */
int greatest_common_divisor(int m,int n)
{
int gcd=1,i;
for(i=0;i<10;i++)
{
if(m%2==0 && n%2==0)
{
gcd = gcd*2;
m=m/2;
n=n/2;
}
else if(m%3==0 && n%3==0)
{
gcd = gcd*3;
m=m/3;
n=n/3;
}
else if(m%5==0 && n%5==0)
{
gcd = gcd*5;
m=m/5;
n=n/5;
}
else if(m%7==0 && n%7==0)
{
gcd = gcd*7;
m=m/7;
n=n/7;
}
else
i=10;
}
return gcd;
}
/* * 该函数用来计算两个数的最小公倍数 */
int least_common_multiple(int m,int n)
{
int lcm=0;
lcm = m*n/greatest_common_divisor(m,n);
return lcm;
}
测试
测试输入
12 4
输出
两个数的最大公约数为:4,最小公倍数为:12
边栏推荐
- Markdown font color editing teaching
- Applet Web Capture -fiddler
- Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
- Attack and defense world misc practice area (GIF lift table ext3)
- 【指针】统计一字符串在另一个字符串中出现的次数
- 线程的实现方式总结
- Sentinel overall workflow
- Tencent map circle
- Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
- Intranet information collection of Intranet penetration (I)
猜你喜欢
随机推荐
搭建域环境(win)
SQL注入
A complete collection of papers on text recognition
Sword finger offer 23 - print binary tree from top to bottom
Constants, variables, and operators of SystemVerilog usage
Bing Dwen Dwen official NFT blind box will be sold for about 626 yuan each; JD home programmer was sentenced for deleting the library and running away; Laravel 9 officially released | Sifu weekly
记一次,修改密码逻辑漏洞实战
Hackmyvm target series (1) -webmaster
Intel oneapi - opening a new era of heterogeneity
Database monitoring SQL execution
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Record an API interface SQL injection practice
【指针】八进制转换为十进制
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
图书管理系统
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
This article explains in detail how mockmvc is used in practical work
Windows platform mongodb database installation
Realize applet payment function with applet cloud development (including source code)