Last active
December 23, 2018 08:34
-
-
Save yjfvictor/5666d5ba237545f0595d to your computer and use it in GitHub Desktop.
求两个数的最大公约数和最小公倍数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* gcd_lcm.c - a program for greatest common divisors and least common multiples | |
* 用于计算最大公约数和最小公倍数的程序 | |
* | |
* Written in 2014 by yjf_victor | |
* | |
* To the extent possible under law, the author has dedicated all copyright | |
* and related and neighboring rights to this software to the public domain | |
* worldwide. This software is distributed without any warranty. | |
* | |
* You should have received a copy of the CC0 Public Domain Dedication along with | |
* this software. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>. | |
* | |
*/ | |
#include <stdio.h> | |
/** | |
* @brief 求两个数的最大公约数 | |
* @note 算法使用欧几里德辗转相除法 | |
* | |
* @param[in] m 第一个数字 | |
* @param[in] n 第二个数字 | |
* | |
* @return 这两个数的最大公约数 | |
*/ | |
int gcd(int m, int n) | |
{ | |
int r; | |
do | |
{ | |
r = m % n; | |
m = n; | |
n = r; | |
} while (r != 0); | |
return m; | |
} | |
/** | |
* @brief 求两个数的最小公倍数 | |
* | |
* @param[in] m 第一个数 | |
* @param[in] n 第二个数 | |
* | |
* @return 这两个数的最小公倍数 | |
*/ | |
int lcm(int m, int n) | |
{ | |
return m / gcd(m, n) * n; | |
} | |
/** | |
* @brief 主函数 | |
* @return 0 | |
*/ | |
int main(void) | |
{ | |
int a, b; | |
scanf("%d%d", &a, &b); | |
printf("最大公约数是:%d\n", gcd(a, b)); | |
printf("最小公倍数是:%d\n", lcm(a, b)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment