关闭

题目1439:Least Common Multiple

366人阅读 评论(0) 收藏 举报
题目1439:Least Common Multiple

时间限制:1 秒

内存限制:128 兆

特殊判题:

题目描述:

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, the LCM of 5, 7 and 15 is 105.

输入:

Input will consist of multiple problem instances. The first line of the input will contain a single integer indicating the number of problem instances. Each instance will consist of a single line of the form m n1 n2 n3 ... nm where m is the number of integers in the set and n1 ... nm are the integers. All integers will be positive and lie within the range of a 32-bit integer.

输出:

For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.

样例输入:
2
3 5 7 15
6 4 10296 936 1287 792 1
样例输出:
105
10296
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;

 int gcd (int a,int b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

int lcm(int a,int b)
{
    return (a/gcd(a,b))*b;//使用了一个技巧,先除后乘,避免溢出
}

int main()
{
    int n;
    long long aim;
    while(scanf("%d",&n)!=EOF)
    {
        long long a[100];
        while(n--)
        {
            int len;
            cin>>len;
            for(int i=0; i<len; i++)
            {
                cin>>a[i];
            }
            aim=a[0];
            for(int i=0; i<len; i++)
            {
                aim=lcm(aim,a[i]);
            }
            printf("%lld\n",aim);
        }
    }
    return 0;
}

 

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

最小公倍数(Least Common Multiple)

最小公倍数=两个数的乘积/两个数的最大公约数。 接上篇求最大公约数方法,最小公倍数的代码如下: public class LCM { //最小公倍数=两数乘积/最大公约数 public sta...
  • foreverbu
  • foreverbu
  • 2014-07-15 14:44
  • 3199

UVA 10791 Minimum Sum LCM(质因数分解+数学分析)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multip...
  • u013555159
  • u013555159
  • 2016-08-03 11:34
  • 222

HDU 1019 Least Common Multiple

Problem Description The least common multiple (LCM) of a set of positive integers is the smallest p...
  • u012796139
  • u012796139
  • 2014-09-27 19:41
  • 390

Least Common Multiple

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot...
  • zchlww
  • zchlww
  • 2015-01-11 10:42
  • 442

HDOJ——1019:Least Common Multiple(求n个数的最小公倍数)

题目就不说了,就是求n个数的最小公倍数。其实这个问题已经困扰我好久了,今天终于有机会把它解决了。 这道题用到了递归和辗转相除法。 n个数的最小公倍数,可以先求前两个数的最小公倍数(lcm),然后再用前...
  • ACMer_hades
  • ACMer_hades
  • 2015-02-01 22:09
  • 1063

三个数的最小公倍数LCM (least common multiple)

问题引入—— 问题描述 已知一个正整数 N,问从 1~N 中任选出三个数,他们的最小公倍数最大可以为多少。 输入格式 输入一个正整数 N。 输出格式 输出一个整数,表示你找到的最小公倍数。 样例输入 ...
  • puppylpg
  • puppylpg
  • 2015-02-06 15:36
  • 1143

Least common multiple HDU - 3092题解

Least common multiple HDU - 3092题解
  • hsj970319
  • hsj970319
  • 2017-02-08 21:44
  • 341

杭电acm 1019 Least Common Multiple

Problem Description The least common multiple (LCM) of a set of positive integers is the smallest p...
  • yuzhiwei1995
  • yuzhiwei1995
  • 2015-05-30 17:57
  • 247

杭电 1019 Least Common Multiple(最小公倍数)

Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java...
  • Ice_Alone
  • Ice_Alone
  • 2014-08-16 22:15
  • 507

【九度OJ】题目1439:Least Common Multiple 解题报告

【九度OJ】题目1439:Least Common Multiple 解题报告标签(空格分隔): 九度OJ原题地址:http://ac.jobdu.com/problem.php?pid=1439题目...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2017-03-07 10:49
  • 167
    个人资料
    • 访问:59077次
    • 积分:1236
    • 等级:
    • 排名:千里之外
    • 原创:149篇
    • 转载:8篇
    • 译文:0篇
    • 评论:5条
    最新评论