题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)
题目链接:http://ac.jobdu.com/problem.php?pid=1439
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
// // 1439 Least Common Multiple.cpp // Jobdu // // Created by PengFei_Zheng on 10/04/2017. // Copyright © 2017 PengFei_Zheng. All rights reserved. // #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <cmath> using namespace std; //to avoid overflow thus use long long to store data long long gcd(long long a, long long b){ return b==0 ? a : gcd(b,a%b); } long long lcm(long long a, long long b){ return (a/gcd(a,b))*b;// not a*b/gcd(a,b) beacuse it may cause overflow } int main(){ int kase; scanf("%d",&kase);//kase number while(kase--){//a small tips int m; scanf("%d",&m); long long ans; scanf("%lld",&ans);// input the first number for(int i = 1 ; i < m ; i++){ long long tmp;//to store the next number scanf("%lld",&tmp); ans = lcm(ans,tmp);//calculate each two adjacent elements's lcm } printf("%lld\n",ans);//print the answer } return 0; }
作者: 伊甸一点
出处: http://www.cnblogs.com/zpfbuaa/
本文版权归作者伊甸一点所有,欢迎转载和商用(须保留此段声明),且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
原文链接 如有问题, 可邮件(zpflyfe@163.com)咨询.