HDOJ 1019 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
2
3 5 7 15
6 4 10296 936 1287 792 1
Sample Output
105
10296
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int a[] = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
int k = a[0];
for(int i=1;i<n;i++){
int b = a[i];
if(k>b){
b=b^k;
k=b^k;
b=b^k;
}
k=su(b,k,mod(b,k));
}
System.out.println(k);
}
}
//最小公倍数。
private static int su(int b, int k, int mod) {
return k*(b/mod);
}
//求最大公约数,辗转相除法。b>k
private static int mod(int b, int k) {
while(k>0){
int m = b%k;
b=k;
k=m;
}
return b;
}
}
HDOJ 1019 Least Common Multiple(最小公倍数问题)的更多相关文章
- HDU 1019 Least Common Multiple【gcd+lcm+水+多个数的lcm】
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- ACM hdu 1019 Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- 1019 Least Common Multiple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- hdu_1019Least Common Multiple(最小公倍数)
太简单了...题目都不想贴了 //算n个数的最小公倍数 #include<cstdio> #include<cstring> #include<algorithm> ...
- zoj1797 Least Common Multiple 最小公倍数
Least Common Multiple Time Limit: 2 Seconds Memory Limit: 65536 KB The least common multiple (L ...
- 杭电1019 Least Common Multiple【求最小公倍数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...
- HDU 1019 Least Common Multiple 数学题解
求一组数据的最小公倍数. 先求公约数在求公倍数.利用公倍数,连续求全部数的公倍数就能够了. #include <stdio.h> int GCD(int a, int b) { retur ...
- HDU 1019 Least Common Multiple GCD
解题报告:求多个数的最小公倍数,其实还是一样,只需要一个一个求就行了,先将答案初始化为1,然后让这个数依次跟其他的每个数进行求最小公倍数,最后求出来的就是所有的数的最小公倍数.也就是多次GCD. #i ...
- HDU - 1019 - Least Common Multiple - 质因数分解
http://acm.hdu.edu.cn/showproblem.php?pid=1019 LCM即各数各质因数的最大值,搞个map乱弄一下就可以了. #include<bits/stdc++ ...
随机推荐
- UI实时预览最佳实践(转)
UI实时预览最佳实践 概要:Android中实时预览UI和编写UI的各种技巧.本文的例子都可以在结尾处的示例代码中看到并下载.如果喜欢请star,如果觉得有纰漏请提交issue,如果你有更好的点子可以 ...
- sublime 3 3083验证码
Sublime Text 3注册码两枚: ----- BEGIN LICENSE ----- K- Single User License EA7E- 3A099EC1 C0B5C7C5 33EBF0 ...
- HighCharts基本用法
var options={ chart: {type: 'column',renderTo: 'ChartDesigner1'},//type :图表类型(柱状图,饼状图),renderTo :指向页 ...
- jsonp Ajax跨域请求
什么是JSONP? JSONP(JSON with Padding)是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域 ...
- cxf WebService整理 (基于注解)
http://blog.csdn.net/zjw10wei321/article/details/39889823
- [!] CocoaPods was not able to update the `master` repo...
输入pod install之后出现: [!] CocoaPods was not able to update the `master` repo. If this is an unexpected ...
- 试用ubuntu-12.04.3-desktop-amd64
由于工作需要,终于要开始使用大名鼎鼎的ubuntu了,从网上下了个ubuntu-12.04.3-desktop-amd64,通过vmware安装,过程相当顺利,只是装完后重启动,发现回到了命令行模式. ...
- 自动Telnet远程登陆命令
有些时候,在面对开发机的时候,不断的telnet和不断的command自己的命令确实非常麻烦,需要一些自动测试或者自动部署的需求.然而面对telnet很多同学都跟我一样一开始觉得无法通过管道等传用户名 ...
- Spring4.0学习笔记(7) —— 通过FactoryBean配置Bean
1.实现Spring 提供的FactoryBean接口 package com.spring.facoryBean; import org.springframework.beans.factory. ...
- php的session实现
对于两次http请求,如果第一次http请求的重要数据要被第二次请求获取,办法是将第一次http请求数据保存下来,保存的办法很多,大体上有使用数据库,缓存,文件等等,那么php中的session实现实 ...