hdu 1019 least common multiple
来源:互联网 发布:js 修改style left 编辑:程序博客网 时间:2024/06/08 02:15
least common multiple
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
23 5 7 156 4 10296 936 1287 792 1
Sample Output
10510296
思路:每两个数字求一次最大公约数,求最大公倍数的时侯要注意,见代码
<span style="font-family:Microsoft YaHei;">#include<stdio.h>int gcd(int a,int b){ int r; for(r=a%b;r!=0;r=a%b) { a=b; b=r; } return b;}int num[10100];int main(){ int n,m,i,j; int sum; while(scanf("%d",&n)!=EOF) { while(n--) { scanf("%d",&m); for(i=0;i<m;i++) scanf("%d",&num[i]); for(i=1;i<=m-1;i++) { sum=gcd(num[i],num[i-1]); num[i]=(num[i]/sum)*num[i-1];//此处不能用这种方法num[i]=(num[i]*num[i-1])/sum,会超出int型的数据范围, } printf("%d\n",num[m-1]); } } return 0;}</span>
0 0
- HDU 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU-1019 least common multiple
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- hdu 1019Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- Hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU 1019Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- HDU 1019 Least Common Multiple
- hdu 1019 Least Common Multiple
- SuperMap iObjects .NET 制作2.5D数据详解(一)
- 杭电1022 Train Problem I
- 非常不错的NoSQL 数据库学习资料汇总
- 单兵应用行业
- hpu 1022&&nyoj 150 Train Problem I 【栈】
- hdu 1019 least common multiple
- Android 开发笔记——通过 Intent 传递类对象
- android app系统签名以及adb安装卸载等简单命令使用
- Xcode 快捷键
- GTK常用控件之标签( GtkLabel )
- Java中的可变参数(来自《Java编程手记》一书)
- 关于FileItem
- android的“播放引擎stagefright”中“OMX文件解码”
- java 网络编程