华为在线机试训练
求最小公倍数
Question
正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。
输入描述:
输入两个正整数A和B。
输出描述:
输出A和B的最小公倍数。
实例
- 输入
- 5,7
- 输出
- 35
Ans
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <iostream> int gys(int lhs,int rhs)//最大公约数 { while(lhs != rhs) { if(lhs > rhs) lhs -= rhs; else rhs -= lhs; } return lhs; } int gbs(int lhs,int rhs)//最小公倍数 { return lhs*rhs/gys(lhs,rhs); } int main() { int lhs = 0; int rhs = 0; std::cin>> lhs; std::cin>> rhs; auto result = gbs(lhs,rhs); std::cout << result << std::endl; } |
本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!
你可能也喜欢
- ♥ 【AcWing 语法基础课 第四讲】02/23
- ♥ 【AcWing 语法基础课 第三讲】02/22
- ♥ 【LeetCode-Mar-链表一】03/24
- ♥ 【AcWing 语法基础课 第七八讲】03/03
- ♥ 【AcWing 语法基础课 第五讲】02/28
- ♥ 【AcWing 语法基础课 第二讲】02/17