当前位置:网站首页>函数:求两个正数的最大公约数和最小公倍
函数:求两个正数的最大公约数和最小公倍
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
边栏推荐
- High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
- 《统计学》第八版贾俊平第三章课后习题及答案总结
- 浙大版《C语言程序设计实验与习题指导(第3版)》题目集
- Interview Essentials: what is the mysterious framework asking?
- {1,2,3,2,5}查重问题
- MSF generate payload Encyclopedia
- Library management system
- Xray and Burp linked Mining
- Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
- Intranet information collection of Intranet penetration (5)
猜你喜欢

网络层—简单的arp断网

Markdown font color editing teaching

Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)

servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。

《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案

《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案

Uibutton status exploration and customization

外网打点(信息收集)

Detailed explanation of network foundation routing

1.支付系统
随机推荐
Wu Enda's latest interview! Data centric reasons
Xray and burp linkage mining
Based on authorized access, cross host, and permission allocation under sqlserver
SystemVerilog discusses loop loop structure and built-in loop variable I
【指针】求字符串的长度
Always of SystemVerilog usage_ comb 、always_ iff
flask实现强制登陆
Hcip -- MPLS experiment
Record once, modify password logic vulnerability actual combat
【指针】求解最后留下的人
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
【指针】八进制转换为十进制
网络层—简单的arp断网
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
【指针】查找最大的字符串
《统计学》第八版贾俊平第十二章多元线性回归知识点总结及课后习题答案
《统计学》第八版贾俊平第六章统计量及抽样分布知识点总结及课后习题答案
WEB漏洞-文件操作之文件包含漏洞
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
浅谈漏洞发现思路