当前位置:网站首页>函数:求两个正数的最大公约数和最小公倍
函数:求两个正数的最大公约数和最小公倍
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
边栏推荐
- Windows platform mongodb database installation
- 内网渗透之内网信息收集(四)
- Tencent map circle
- Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
- Xray and Burp linked Mining
- 《统计学》第八版贾俊平第八章假设检验知识点总结及课后习题答案
- How to earn the first pot of gold in CSDN (we are all creators)
- Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
- Constants, variables, and operators of SystemVerilog usage
- . Net6: develop modern 3D industrial software based on WPF (2)
猜你喜欢

Wu Enda's latest interview! Data centric reasons

Interpretation of iterator related "itertools" module usage

Record an edu, SQL injection practice

Chain team implementation (C language)

Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary

Tencent map circle

Web vulnerability - File Inclusion Vulnerability of file operation

Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class
![New version of postman flows [introductory teaching chapter 01 send request]](/img/0f/a41a39093a1170cc3f62075fd76182.jpg)
New version of postman flows [introductory teaching chapter 01 send request]

移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
随机推荐
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
小程序web抓包-fiddler
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
The difference between layer 3 switch and router
记一次edu,SQL注入实战
"Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
Wei Shen of Peking University revealed the current situation: his class is not very good, and there are only 5 or 6 middle-term students left after leaving class
Which is more advantageous in short-term or long-term spot gold investment?
【指针】求字符串的长度
[paper reproduction] cyclegan (based on pytorch framework) {unfinished}
使用 flask_whooshalchemyplus jieba实现flask的全局搜索
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
DVWA (5th week)
Chain team implementation (C language)
内网渗透之内网信息收集(五)
Hackmyvm target series (4) -vulny
安全面试之XSS(跨站脚本攻击)