首页 > 代码库 > 题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)
题目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 datalong 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;}
题目1439:Least Common Multiple(求m个正数的最小公倍数lcm)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。