zoukankan      html  css  js  c++  java
  • ACM Least Common Multiple

    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. 

     

    InputInput 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. 
    OutputFor each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer. 
    Sample Input

    2
    3 5 7 15
    6 4 10296 936 1287 792 1

    Sample Output

    105
    10296
     1 #include<bits/stdc++.h>
     2 using namespace  std;
     3 long long solve(long long a,long long b)
     4 {
     5     long long m,n,c;
     6     m = a, n = b;
     7     a = max(m,n);
     8     b = min(m,n);
     9     while(b)
    10     {
    11         c = a % b;
    12         a = b;
    13         b = c;
    14     }
    15     return m*n/a;
    16 }
    17 int main()
    18 {
    19     long long T,n,a,b;
    20     while(cin>>T)
    21     {
    22         while(T--)
    23         {
    24             scanf("%d",&n);
    25             scanf("%d",&a);
    26             for(int i = 0; i < n-1; i++)
    27             {
    28                 scanf("%d",&b);
    29                 a = solve(a,b);        
    30             }    
    31             cout<<a<<endl;
    32         }
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    linux中~和/的区别
    Linux centos 7安装
    xshell远程连接虚拟机
    虚拟机Linux不能上网简单有效的解决办法
    visudo
    users
    TreeSizeFree(硬盘文件整理)
    dos2unix
    iconv
    PS1系统变量
  • 原文地址:https://www.cnblogs.com/jj81/p/7356821.html
Copyright © 2011-2022 走看看