Lowest Common Multiple Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13323 Accepted Submission(s): 5486
Problem Description
求n个数的最小公倍数。
Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。
Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。
Sample Input
2 4 6
3 2 5 7
Sample Output
12
70
Author
lcy
Source
C语言程序设计练习(五)
#include <stdio.h>
/*
//求最大公约数的一般式
void gcd(int a, int b)
{
int m, n, temp;
m = a;
n = b;
if(a < b)
{
m = b;
n = a;
}
do{
temp = m % n;
m = n;
n = temp;
}while(n);
return m;
}*/
//求最大公约数-是辗转相除法的递归式
int gcd(int a, int b)
{
return b ? gcd(b, a % b): a;
}
/*
int gcd(int a, int b)
{
int r;
while(r = a % b) a = b, b = r;
return b;
}*/
//求最小公倍数
int lcm(int a, int b)
{
//不要写为 a * b / gcd(a, b) 要溢出
return a / gcd(a, b) * b;
}
int main()
{
int n, l, m;
while(scanf("%d", &n) != EOF)
{
l = 1;
while(n--)
{
scanf("%d", &m);
l = lcm(m, l);
}
printf("%d\n", l);
}
return 0;
}
分享到:
相关推荐
Least Common Multiple Problem Description The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, ...
杭电OJ 2028代码 the rosolve of the hdu 2028
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
搜索 dfs 解题代码 hdu1241
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
HDU最全ac代码
自己做的HDU ACM已经AC的题目
hdu动态规划算法集锦
hdu题目分类
HDU图论题目分类