当前位置:网站首页>最大公约数
最大公约数
2022-07-24 04:33:00 【 嗨害嗨】
系列文章目录
1.什么是公约数?
公约数,亦称“公因数”。 它是一个能同时整除几个整数的数 。 如果一个整数同时是几个整数的 约数 ,称这个整数为它们的“公约数”。
2.最大公约数
公约数中最大的称为最大公约数。 对任意的若干个正整数,1总是它们的公因数。 公约数与公倍数相反,就是既是A的约数同时也是B的约数的数,12和15的公约数有1,3,最大公约数就是3。 再举个例子,30和40,它们的公约数有1,2,5,10,最大公约数是10
3.最大公约数和最小公倍数的关系:
两个数的乘积/最大公约数=最小公倍数
4.解题引导
如18和6,我们可以知道两个数的最大公约数一定小于等于其中最小的那个数,那么要想实现最大公约数,必须先找出两个数中的最小值
然后再从6或比6小的数中寻找最小公约数

5.代码展示:
代码如下(示例):
#include<stdio.h>
int main() {
int m = 0;
int n = 0;
scanf("%d %d", &m, &n);
int min = (m < n ? m : n);
while (1)
{
if (m % min == 0 && n % min == 0)
{
printf("%d", min);
break;
}
min--;
}
return 0;
}
当然方法不只这一种,这种方法效率比较低
6.辗转相除法
介绍如图:
如图,用24除18取余数6
用18除6 取余为0
6就是这两个数的最大公约数
如上图如果我们把24看作m,把18看作n,余数如果不是0,就将n的值赋给m,余数的值赋给n
余数如果是0,n就是最大公约数
7.代码演示:
#include<stdio.h>//最大公约数
int main() {
int m = 0;
int n = 0;
int r = 0;
scanf("%d %d", &m, &n);
while (r = m % n)
{
m = n;
n = r;
}
printf("%d", n);
return 0;
}
总结
这里所涉及的内容都比较基础,如果以上代码中有些语法不知道,说明基础太过薄弱
可以阅读我的系列文章【C】 C语言入门
当然只要有了明确的思路,这种代码还是非常好写的。
边栏推荐
- These are controlled deployment capabilities, and then cited as
- Ambire wallet opens twitter spaces series
- 到3mm;提供安全稳定的产品作的执行据发出方iid
- 可以脱离设据生效这些都是控化部署能力,而后引如
- Imitate today's headlines real-time news wechat applet project source code
- What if the references in the word sent by others are {} in such a garbled format
- PMIX ERROR: ERROR in file gds_ ds12_ lock_ pthread.c
- Upgrade POI TL version 1.12.0 and resolve the dependency conflict between the previous version of POI (4.1.2) and easyexcel
- NFT除了买卖还能质押?
- C语言:冒泡排序法
猜你喜欢

Will your NFT disappear? Dfinity provides the best solution for NFT storage

Merge sort
![Graduation thesis on enterprise production line improvement [Flexsim simulation example]](/img/83/381ef1566d5a863b709f504b46e169.png)
Graduation thesis on enterprise production line improvement [Flexsim simulation example]

Nautilus 3.19.2为Gnome增添动力
![[network counting experiment report] Cisco LAN Simulation and simple network test](/img/42/c0bd30ae7b693f93075e91645070a5.png)
[network counting experiment report] Cisco LAN Simulation and simple network test

Design of two power dividers and directional couplers for basic experiment of microwave technology

Qt5.14_ Realize the free drag and drop combination function of vs2019 panel under mingw/msvc

Logback log framework technology in project development

C语言经典习题之评委打分去掉最高最低求平均分

Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base
随机推荐
How long has it been since you changed your cell phone?
[untitled]
[hope to answer] the data cannot be synchronized correctly
CONDA common commands
Robustness evaluation of commercial in vivo detection platform
高频小信号谐振放大器设计-课程设计Multisim仿真
Clickpaas, a low code service provider, has completed a strategic merger with BiP technology to jointly build an industrial digital base
J9 number theory: what is Web3.0? What are the characteristics of Web3.0?
链接预测中训练集、验证集以及测试集的划分(以PyG的RandomLinkSplit为例)
Label smoothing
C语言:选择排序法
Basic learning notes of C language
PMIX ERROR: ERROR in file gds_ ds12_ lock_ pthread.c
Can NFT pledge in addition to trading?
What if the references in the word sent by others are {} in such a garbled format
Text attack methods open source code summary
Shell语法(二)
What new opportunities exist in the short video local life section?
How much do you know about thread pool and its application
Qt5.14_MinGW/MSVC下实现VS2019面板自由拖拽组合功能