• CDZSC_2015寒假新人(2)——数学 A


    Description

    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

    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. 
     

    Output

    For 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
     

     题解 :简单的LCM,见代码。

     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #define LL long long
     5 
     6 LL gcd(LL a, LL b)
     7 {
     8     LL r = b;
     9     while (r)
    10     {
    11         r = a % b;
    12         a = b;
    13         b = r;
    14     }
    15     return a;
    16 }
    17 
    18 LL lcm(LL a, LL b)
    19 {
    20     return a * b / gcd(a, b);
    21 }
    22 
    23 int main()
    24 {
    25 #ifdef CDZSC_OFFLINE
    26     freopen("in.txt", "r", stdin);
    27     freopen("out.txt", "w", stdout);
    28 #endif
    29     int t, n;
    30     LL a, b;
    31     scanf("%d", &t);
    32     while (t--)
    33     {
    34         scanf("%d%lld", &n, &a);
    35         while (--n)
    36         {
    37             scanf("%lld", &b);
    38             a = lcm(a, b);
    39         }
    40         printf("%lld\n", a);
    41     }
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    java 验证码
    时间日期转换+两个日期相减
    java创建文件和目录
    java获取文件名的三种方法
    获取当前日期前100天的日期
    字符串去重
    Java,数据库中的数据导入到Excel
    两个list<object> 比较 得到相同数据 差异数据
    Springmvc中@RequestParam传值中文乱码解决方案
    将src非空的属性注入到des中
  • 原文地址:https://www.cnblogs.com/LiuACG/p/4246576.html
Copyright © 2020-2023  润新知