定义:

最大公约数(英语: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)的更多相关文章

  1. 845. Greatest Common Divisor

    描述 Given two numbers, number a and number b. Find the greatest common divisor of the given two numbe ...

  2. hdu 5207 Greatest Greatest Common Divisor 数学

    Greatest Greatest Common Divisor Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/ ...

  3. upc组队赛17 Greatest Common Divisor【gcd+最小质因数】

    Greatest Common Divisor 题目链接 题目描述 There is an array of length n, containing only positive numbers. N ...

  4. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  5. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  6. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

  7. 【Leetcode_easy】1071. Greatest Common Divisor of Strings

    problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...

  8. 2018CCPC桂林站G Greatest Common Divisor

    题目描述 There is an array of length n, containing only positive numbers.Now you can add all numbers by ...

  9. leetcode 1071 Greatest Common Divisor of Strings

    lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以st ...

随机推荐

  1. kafka环境搭建

    kafka环境搭建 for mac 对应qq群号:616961231 在之前的文章中, 有学习能力和兴趣爱好的同学,自己动手维护测试环境,丰衣足食是最好的办法,今天我们来讲讲kafka在mac上的安装 ...

  2. zabbix监控Oracle

    可监控项 使用zabbix监控oracle数据库需要借助第三方的插件,目前使用较多的是orabbix.目前维护到了1.2.3版本.关于oracle自带的监控项目有以下几个: DB Version (i ...

  3. LeetCode算法题-Repeated Substring Pattern(Java实现)

    这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...

  4. 安装mysql的踩坑之旅

    近期的一个项目要求用mysql数据库,正好系统重装了,复习下mysql的安装,哪成想是踩了无数坑啊! 要安装首先自然是火速进官网下个安装包(下载地址https://dev.mysql.com/down ...

  5. vue2.0 :style :class样式设置

    HTML :style 的用法 <el-dialog custom-class="creatUser-wrap" :style="{display:formShow ...

  6. JavaScript—面向对象开发详解和垃圾回收

    面向对象的概述 ECMAScript 有两种开发模式:1.函数式(过程化),2.面向对象(OOP). 面向对象的语言有一个标志,那就是类的概念,而通过类可以创建任意多个具有相同属性和方法的对象.但是, ...

  7. 网络流之P3254 圆桌问题

    题目描述 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,……,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,……,n)个代表就餐. ...

  8. IDEA+Maven+各个分支代码进行合并

    各个模块的分支代码进行合并到一起:https://blog.csdn.net/xsj_blog/article/details/79198502

  9. JQuery 获取select 的value值和文本值

    <select name="month" id="selMonth">    <option value="1">一 ...

  10. JQ面向对象的放大镜

    index.html <!DOCTYPE html><html> <head> <meta charset="utf-8" /> & ...