最大公约数和最小公倍数(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 ...
随机推荐
- Android相机启动crash错误排查
一个Android的用到相机的service程序,在比较少的情况下会出现在系统启动时crash.log显示是在调用ACameraDevice_createCaptureSession时返回错误ACAM ...
- [Python][小知识][NO.5] 使用 Pyinstaller 打包成.exe文件
1.安装 pyinstaller 插件 cmd命令:pip install PyInstaller PS . o.o 不知道 easy_install 的百度吧. 2.pyinstaller 简介 他 ...
- 清清楚楚地搭建MongoDB数据库(以搭建4.0.4版本的副本集为例)
数据的目录文件层次设计 我们一般采用多实例的方式,而不是将所有的数据库尽可能地放在一个实例中. 主要基于以下考虑: 1:不同业务线对应的数据库放在不同的实例上,部分操作的运维时间容易协调等到. 2:相 ...
- centos7查看可登陆用户
一.命令 cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1 cat /etc/passwd | grep /bin/bash | cu ...
- SQLServer的三种Recovery Model
SQL Server恢复模式的三种类型的比较 此文章主要向大家讲述的是SQL Server恢复模式,我们主要介绍的是三种恢复模式,其中包括简单SQL Server数据库的恢复模式.完整恢复模式与大容量 ...
- AD域安装及必要设置
本文主要介绍AD域的安装和程序开发必要的设置. 一.安装AD域 运行dcpromo命令,安装AD域. 步骤: 1.win+R 2.dcpromo 图例: 百度百 ...
- Asp.Net Core 实现服务的批量注册注入
- 【PAT】B1016 部分A+B
水题 以字符和字符串形式储存输入,比较,计算出两个个数的D的个数,用for循环拼成P,相加得出结果 #include<stdio.h> int main(){ char A[20],DA, ...
- Bootstrap -- 插件: 模态框、滚动监听、标签页
Bootstrap -- 插件: 模态框.滚动监听.标签页 1. 模态框(Modal): 覆盖在父窗体上的子窗体. 使用模态框: <!DOCTYPE html> <html> ...
- HTML---标签的分类 | display | visibility
一.HTML标签的分类和转换 1.1,三类HTML标签 1.2,三类HTML标签的特点 1.3,三类标签的转换--display:none隐藏于visibility不同之处 二.HTML某些标签--不 ...