最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义:
最大公约数(英语:greatest common divisor,gcd)。是数学词汇,指能够整除多个整数的最大正整数。而多个整数不能都为零。例如8和12的最大公因数为4。
最小公倍数是数论中的一个概念。若有一个数$$X$$,可以被另外两个数$$A$$、$$B$$整除,且$$X$$大于(或等于)$$A$$和$$B$$,则$$X$$X为$$A$$和$$B$$的公倍数。$$A$$和$$B$$的公倍数有无限个,而所有的公倍数中,最小的公倍数就叫做最小公倍数。两个整数公有的倍数称为它们的公倍数,其中最小的一个正整数称为它们两个的最小公倍数。
辗转相除法:两个整数的最大公约数等于其中较小的数和两数的差的最大公约数。例如,252和105的最大公约数是21 $$ (252=21\times 12;105=21\times 5)$$因为 252 − 105 = 21 × (12 − 5) = 147 ,所以147和105的最大公约数也是21。在这个过程中,较大的数缩小了,所以继续进行同样的计算可以不断缩小这两个数直至其中一个变成零。这时,所剩下的还没有变成零的数就是两数的最大公约数。
运用辗转相除法来计算最大公约数和最小公倍数
#include <stdio.h>
int main()
{
int a, b;
printf("请输入两个数:\n");
scanf("%d%d", &a, &b);
int x = a, y = b;
while (b != 0)
{
int temp = a % b;
a = b;
b = temp;
}
printf("最大公约数:%d\n", a);
printf("最下公倍数:%d\n", x * y / a);
return 0;
}
使用自定义函数来计算最大公约数和最小公倍数
int GCD(int a, int b)
{
if (b)
while ((a %= b) && (b %= a));
return a + b;
}
int LCM(int a, int b)
{
return a * b / GCD(a, b);
}
最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)的更多相关文章
- 845. Greatest Common Divisor
描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...
- hdu 5207 Greatest Greatest Common Divisor 数学
Greatest Greatest Common Divisor Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...
- upc组队赛17 Greatest Common Divisor【gcd+最小质因数】
Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
- 【Leetcode_easy】1071. Greatest Common Divisor of Strings
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...
- 2018CCPC桂林站G Greatest Common Divisor
题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...
- leetcode 1071 Greatest Common Divisor of Strings
lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...
随机推荐
- JVM内存管理 《深入分析java web 技术内幕》第八章
8.1 物理内存与虚拟内存 物理内存RAM(随机存储器),寄存单元为寄存器,用于存储计算单元执行指令的中间结果. 连接处理器和RAM或者处理器和寄存器的是地址总线,这个地址的宽度影响了物理地址的索引范 ...
- Python使用Plotly绘图工具,绘制散点图、线形图
今天在研究Plotly绘制散点图的方法 使用Python3.6 + Plotly Plotly版本2.0.0 在开始之前先说说,还需要安装库Numpy,安装方法在我的另一篇博客中有写到:https:/ ...
- Unity协程的坑
unity终止协程提供了 StopAllCoroutines() 和 StopCoroutines() 两个方法, 但是都只能终止该文件内的 IEnumerator. 并且具体使用有点坑, 见如下实 ...
- centos7操作记录
/root/wang/shell 存放练习的shell文件,快捷命令wsh(alias wsh='cd /root/wang/shell') /root/wang/OS_bak 存放系统备份文件 ...
- SQLServer之删除存储过程
删除存储过程注意事项 在删除任何存储过程之前,请检查依赖对象,并且相应地修改这些对象. 如果没有更新这些对象,则删除存储过程可能会导致依赖对象和脚本失败. 若要显示现有过程的列表,请查询 sys.ob ...
- c/c++ linux 进程间通信系列5,使用信号量
linux 进程间通信系列5,使用信号量 信号量的工作原理: 由于信号量只能进行两种操作等待和发送信号,即P(sv)和V(sv),他们的行为是这样的: P(sv):如果sv的值大于零,就给它减1:如果 ...
- python3 str(字符串)
__add__函数 (在后面追加字符串) s1 ='Hello' s2 = s1.__add__(' boy!') print(s2) #输出:Hello boy! __contains__(判断是否 ...
- sqlserver数据库导出表结构和表数据生成创建表和insert语句
问题描述: 有时候我们只需要导出一张表和表数据到另外一个数据库,如果是备份整个库的话,就会很麻烦那样,没法满足需求. 解决方法: 以sqlserver2014为例:把MGActivity数据库的bat ...
- crontab 详细用法 定时任务
转自:http://blog.chinaunix.net/uid-25785357-id-3434344.html 使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux命 ...
- PHP实现表单提交发送邮件
只需要三个文件就可以了: 注意: 文件自命名需修改表单提交url,包含的类文件名: HTML表单文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...