当前位置:网站首页>函数:求两个正数的最大公约数和最小公倍
函数:求两个正数的最大公约数和最小公倍
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
边栏推荐
- XSS (cross site scripting attack) for security interview
- 记一次edu,SQL注入实战
- Applet Web Capture -fiddler
- 内网渗透之内网信息收集(三)
- 《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- 网络层—简单的arp断网
- Hackmyvm target series (1) -webmaster
- Hackmyvm target series (2) -warrior
- Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
猜你喜欢
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
1.支付系统
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
网络基础之路由详解
小程序web抓包-fiddler
关于超星脚本出现乱码问题
Internet Management (Information Collection)
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
Circular queue (C language)
随机推荐
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
[err] 1055 - expression 1 of order by clause is not in group by clause MySQL
网络层—简单的arp断网
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
The United States has repeatedly revealed that the yield of interest rate hiked treasury bonds continued to rise
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
[issue 18] share a Netease go experience
记一次edu,SQL注入实战
sqqyw(淡然点图标系统)漏洞复现和74cms漏洞复现
Intranet information collection of Intranet penetration (4)
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
小程序web抓包-fiddler
Applet Web Capture -fiddler
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Hackmyvm target series (6) -videoclub
2022华中杯数学建模思路
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
攻防世界MISC练习区(gif 掀桌子 ext3 )
{1,2,3,2,5}查重问题
线程的实现方式总结