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 }
  • 相关阅读:
    酷商城新闻客户端源码
    一款类似塔防类的保卫羊村游戏android源码
    躲避球游戏ios源码
    卡通投掷游戏ios源码
    爱拼图游戏源码完整版
    newsstand杂志阅读应用源码ipad版
    linux下proxy设定的一般方法
    android中调用App市场对自身App评分
    Android AChartEngine
    设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/jj81/p/7356821.html
Copyright © 2011-2022 走看看