定义:

最大公约数(英语: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. C#字符串转二进制、二进制转字符串

    最近公司要做一个操作日志的模块,如果将操作日志以字符串的形式存到后台数据库,非常浪费内存,不可取,特意写了字符串与二进制相互转换的函数. 1.字符串转二进制 private string String ...

  2. 【原】Java学习笔记025 - 内部类

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 内部类(嵌套类): ...

  3. GitHub下载克隆clone指定的分支tag代码

    需求描述: 这边有很多tag分支版本的代码,我想克隆下载指定版本到我服务器上面 例如:我想下载tag:1.4.1的代码 解决方法: 命令:git clone --branch [tags标签] [gi ...

  4. LeetCode算法题-Keyboard Row(Java实现)

    这是悦乐书的第245次更新,第258篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第112题(顺位题号是500).给定一个单词列表,返回可以在美国键盘的一行上使用字母表键 ...

  5. Springboot配置文件解析器

    @EnableScheduling @MapperScan(value = "com.****.dao") @EnableTransactionManagement @Enable ...

  6. Redis管道和发布订阅

    管道:原子性执行命令 ''' redis-py默认在执行每次请求都会创建(连接池申请连接)和断开(归还连接池)一次连接操作, 如果想要在一次请求中指定多个命令,则可以使用pipline实现一次请求指定 ...

  7. Redis操作hash

    来自:http://www.cnblogs.com/alex3714/articles/6217453.html Hash操作 hash表现形式上有些像pyhton中的dict,可以存储一组关联性较强 ...

  8. 【模块04-大数据技术入门】02节-HDFS核心知识

    分布式存储 (1) 5PB甚至更大的数据集怎么存储 ? 所有数据分块,每个数据块冗余存储在多台机器上(冗余可提高数据块高可用性).另外一台机器上启动一个管理所有节点.以及存储在各节点上面数据块的服务. ...

  9. git添加/删除远程仓库

    注意:仓库只有管理员建的你才有权限上传,不然自己建的也没用,没权限上传 1.远程仓库路径查询 git remote -v 2.添加远程仓库 git remote add origin <你的项目 ...

  10. 转://ORA-00603,ORA-27501,ORA-27300,ORA-27301,ORA-27302故障案例一则

    背景介绍: 这是一套windows的rac系统.数据库后台日志报ORA-00474:SMON process terminated with error.接着报ORA-00603,ORA-27501, ...