最小公倍数题(Problem ID:1108)

来源:互联网 发布:永乐大帝 知乎 编辑:程序博客网 时间:2024/05/16 08:48

题址:http://acm.hdu.edu.cn/showproblem.php?pid=1108


Problem Description
给定两个正整数,计算这两个数的最小公倍数。


Input
输入包含多组测试数据,每组只有一行,包括两个不大于1000的正整数.


Output
对于每个测试用例,给出这两个数的最小公倍数,每个实例输出一行。


Sample Input
10 14


Sample Output
70

简单题,函数cal(a,b)中辗转相除法求a和b的最小公约数,然后a*b/cal(a,b)即为a和b的最小公倍数。


AC代码:

#include<iostream>#include<stdio.h>using namespace std;int cal(int a,int b){int i,c;c=a%b;while(c!=0){a=b;b=c;c=a%b;}return b;}int main(){int a,b,gbs;while(cin>>a>>b){gbs=a*b/cal(a,b);cout<<gbs<<endl;}return 0;}


0 0