n个数的最小公倍数

Description

求n个数的最小公倍数。

Input

输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。

Output

为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。

Sample Input

2 4 6

3 2 5 7

Sample Output

12

70

#include<stdio.h>
int GCD(int num, int x)
{
    if(num%x==0)
        return x;
    return GCD(x, num%x);
}
int main ()
{
    int n, x;
    long long  num;
    while (scanf("%d", &n)!= EOF)
    {
        num = 1;
        while (n--)
         {
             scanf ("%d", &x);
             num = x/GCD(num,x)*num;
         }
         printf("%lld\n", num);
    }

    return 0;
}
时间: 2024-08-24 23:01:27

n个数的最小公倍数的相关文章

HDOJ-ACM1019(JAVA) 多个数的最小公倍数

题意:求多个数的最小公倍数 很简单,但是我一开始的做法,估计会让结果越界(超过int的最大值) import java.util.*; import java.io.*; public class Main{ public static void main(String[] arg){ Scanner scan = new Scanner(new BufferedInputStream(System.in)); int n =scan.nextInt(); int[] nums = new in

求n个数的最小公倍数(数值范围的控制)

Description 求n个数的最小公倍数. INPUT 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. OUTPUT 为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行.你可以假设最后的输出是一个32位的整数. SAMPLE INPUT 2 4 6 3 2 5 7 SAMPLE OUTPUT 12 70 解题心得: 本来是很简单的题的,可是由于没有适当的控制算法,导致结果溢出. AC代码: 1 #include<stdio.h> 2 long int

两个数的最小公倍数

问题: 求两个数的最小公倍数 #include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { int m, n, b; int temp, k; int lcw; scanf(&q

求N个数的最小公倍数

[求N个数的最小公倍数] 1.两两依次求解+提取公因数法. 2.质因数分解法. 例题 2.提取部分公因数法. 3.倍数Trick. 4.幂次Trick.

projecteuler----&gt;problem=5----Smallest multiple n个数求最小公倍数

title: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 翻译: 2520是能被1到10的自然数整除的最小正整数. 那么,能被1到20

hdu 1788(多个数的最小公倍数)

Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2415    Accepted Submission(s): 997 Problem Description 我知道部分同学最近在看中国剩余定理,就这个定理本身,还是比较简单的:假设m1,m2,…,mk两两互素,则下面同余方程

求多个数的最小公倍数的问题

Problem Description 求n个数的最小公倍数. Input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output 为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行.你可以假设最后的输出是一个32位的整数. Sample Input 2 4 6 3 2 5 7 Sample Output 12 70 #include <stdio.h> #include <stdlib.h> typedef long unsigned

HDU 1019 (多个数的最小公倍数)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 61592    Accepted Submission(s): 23486 Problem Description The least comm

贪心算法之随机三个数的最小公倍数

问题描述 已知一个正整数N,问从1~N中任选出三个数,他们的最小公倍数最大可以为多少. 输入格式 输入一个正整数N. 输出格式 输出一个整数,表示你找到的最小公倍数. 样例输入 9 样例输出 504 数据规模与约定 1 <= N <= 106. 代码: import java.util.Scanner; public class Main {    public static void main(String[] args) {        Scanner in=new Scanner(Sy