***************************************转载请注明出处:http://blog.csdn.net/lttree***************************************

Least Common Multiple

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 28975    Accepted Submission(s): 10905

Problem 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
  1. 2
  2. 3 5 7 15
  3. 6 4 10296 936 1287 792 1
 
Sample Output
  1. 105
  2. 10296
 
Source
 

这道题就是求一堆数的最小公倍数。

要注意,每一行的第一个数表示后面跟着有几个数。

然后,我的方法是将输入的数存到数组,然后进行从大到小排序。

从头開始推断,假设当前的mul(之前全部数的最小公倍数)是当前数的倍数,就不须要计算这个数了,直接跳过。

最后再注意一点,用__int64来解决,scanf是用:%I64d

  1. /****************************************
  2. *****************************************
  3. * Author:Tree *
  4. *From : blog.csdn.net/lttree *
  5. * Title : Least Commn Multiple *
  6. *Source: hdu 1019 *
  7. * Hint : *
  8. *****************************************
  9. ****************************************/
  10.  
  11. #include <stdio.h>
  12. #include <algorithm>
  13. using namespace std;
  14.  
  15. __int64 num[10001];
  16.  
  17. // 求a和b的最小公倍数
  18. __int64 lcm( __int64 a,__int64 b)
  19. {
  20. __int64 x=a,y=b,k;
  21. while( x%y!=0 )
  22. {
  23. k=x%y;
  24. x=y;
  25. y=k;
  26. }
  27. return a*b/k;
  28. }
  29. // 从大到小排序 sort用
  30. bool cmp(__int64 a,__int64 b)
  31. {
  32. return a>b;
  33. }
  34. int main()
  35. {
  36. int t,n,i;
  37. __int64 mul;
  38.  
  39. scanf("%d",&t);
  40. while( t-- )
  41. {
  42. scanf("%d",&n);
  43. for( i=0;i<n;++i )
  44. scanf("%I64d",&num[i]);
  45. sort(num,num+n,cmp);
  46. mul=num[0];
  47. // 假设当前的最小公倍数是该数的倍数就不须要计算了。
  48. for( i=1;i<n;++i )
  49. if( mul%num[i]!=0 )
  50. mul=lcm(mul,num[i]);
  51. printf("%I64d\n",mul);
  52. }
  53. return 0;
  54. }

ACM-简单题之Least Common Multiple——hdu1019的更多相关文章

  1. 2道acm简单题(2010):1.猜数字游戏;2.字符串提取数字并求和;

    //第一题是猜数字的游戏.//题目:随即产生一个3位的正整数,让你进行猜数字,//如果猜小了,输出:"猜小了,请继续".//如果猜大了,输出:"猜大了,请继续" ...

  2. 2道acm简单题(2013):1.(时分秒)时间相减;2.主持人和N-1个人玩游戏,每个人说出自己认识的人数,判断其中是否有人说谎。

    /*1.题目:输入一个数,代表要检测的例子的个数,每个例子中:输入两个时间(格式HH:MM : SS),前面时间减去后面时间,输出在时钟上显示的时间,格式一样,如果是以为数字的前面补零.*//**思路 ...

  3. 3道acm简单题(2011):1.判断是否能组成三角形;2.判断打鱼还是晒网;3.判断丑数。

    //1.输入三个正整数A.B.C,判断这三个数能不能构成一个三角形.//思路:最小的两边之和是否是大于第三边#include<iostream>#include<algorithm& ...

  4. HDU1019 Least Common Multiple(多个数的最小公倍数)

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  5. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  6. HDU-1019 Least Common Multiple

    http://acm.hdu.edu.cn/showproblem.php?pid=1019 Least Common Multiple Time Limit: 2000/1000 MS (Java/ ...

  7. ACM hdu 1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  8. acm.njupt 1001-1026 简单题

    点击可展开上面目录 Acm.njupt 1001-1026简单题 第一页许多是简单题,每题拿出来说说,没有必要,也说不了什么. 直接贴上AC的代码.初学者一题题做,看看别人的AC代码,寻找自己的问题. ...

  9. ACM学习历程—HDU 3092 Least common multiple(数论 && 动态规划 && 大数)

    Description Partychen like to do mathematical problems. One day, when he was doing on a least common ...

随机推荐

  1. 开发环境FAQ

    问: Win7下VS2008无法识别WinCE5.0SDK 答:拷贝WinXP环境(安装过VS2008 + WinCE5.0SDK)下C:/Program Files/Microsoft Visual ...

  2. 基于unity3d和leap motion的拼图游戏

    近期用unity3d引擎做了一个拼图游戏,会分几次写完,以此作为总结. 本文基本查找了网上能查到的全部资料作为參考.也算是大家节省了时间. 眼下仅仅完毕了拼图部分,leap motion手势控制部分会 ...

  3. Servlet的学习之Response响应对象(2)

    本篇接上一篇<Servlet的学习之Response响应对象(1)>,继续从HttpServletResponse响应对象来介绍其方法和功能. 使用setHeader方法结合HTTP协议的 ...

  4. HDU 4876 ZCC loves cards _(:зゝ∠)_ 随机输出保平安

    GG,,,g艹 #include <cstdio> #include <iostream> #include <algorithm> #include <st ...

  5. qsettings 保存自定义结构体(QVariant与自定义结构体相互转化)

    参考博文:QVariant与自定义数据类型转换的方法. 这里摘取其关键内容: 1.将自定义数据类型使用Q_DECLARE_METATYPE宏进行声明,便于编译器识别. 2.在插入对象的时候,声明QVa ...

  6. 移动开发的框架(用Firepower,不用listview,超快) good

    我是通过http传送xml后台是阿帕奇的http server,后台可以用delphi或php 都可以.用post 刚才试了试自带的TNetHttpClient,感觉还好,代码封装也不算深,收发数据也 ...

  7. Dom4j SAXReader Constructors

    Dom4j读取xml:eg1: package xml; import java.io.File; import org.dom4j.DocumentException; import org.dom ...

  8. web.xml中listener作用及使用

    一.WebContextLoaderListener 监听类 它能捕捉到server的启动和停止,在启动和停止触发里面的方法做对应的操作! 它必须在web.xml 中配置才干使用,是配置监听类的 二. ...

  9. My Emacs For Common Lisp

    My Emacs For Common Lisp My Emacs For Common Lisp

  10. git for windows (又名 msysgit)如何记住用户名和密码

    创建存储用户名密码的文件 在home文件夹,一般是 C:\Documents and Settings\Administrator 下建立文件 .git-credentials (windows下不允 ...