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++ ...
随机推荐
- 第一篇:Power BI数据可视化概述
前言 "可视化之工具,可爱者甚蕃.统计学家独爱R,自Python来,世人盛爱matplotlib.余独爱Power BI之出微软而不染(免费),濯Office而不妖(够精简).......& ...
- 转载:C#中的多态
原文地址 http://www.cnblogs.com/jhxk/articles/1644018.html 感谢博主分享! 之前看到过类似的问题:如果面试时主考官要求你用一句话来描述多态,尽可能的 ...
- Modernizr——为HTML5和CSS3而生!
原文地址:http://www.alistapart.com/articles/taking-advantage-of-html5-and-css3-with-modernizr/ 堂主译文地址:ht ...
- DEDECMS织梦列表页每隔N行文章添加一条分隔线
这是给一个朋友做模板的时候,用到的一个小小的技巧,今天正好用上了,以前看到有人问过不知道解决没有,今天整理了一下,本想保存在自己的电脑里,后来一想,不如咱们一起共享一下,也是对织梦的感恩,有好东西就来 ...
- My.Ioc 代码示例——实现自动注册/解析
在很多 Ioc 容器中,当使用者向容器请求实现了某个契约类型 (Contract Type) 的服务时 (调用类似如下方法 container.Resolve(Type contractType)), ...
- 关于svn获取获取文件时 Unable to connect to a repository at URL"https://..."执行上下文错误:参数错误
错误提示: 下面的六种解决方案都未能解决: 1.不提示输入用户名和密码,不管重装多少次都一样. 2.TortoiseSVN的setting->Saved Data->Authenticat ...
- iOS之多线程开发NSThread、NSOperation、GCD
原文出处: 容芳志的博客 欢迎分享原创到伯乐头条 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Centr ...
- 重新开始学习javase_隐藏实施过程
一.隐藏实施过程 对于隐藏实施过程,thinking in java中讲了很好,无非就是一个好的程序尽量做到,对外公开的程序,即使内部程序发生变动,也不会影响这些公开的服务的使用 类的导入java中的 ...
- 【windows开发实现记事本程序——逻辑篇1】
1. 主要内容 从本节开始介绍windows开发实现记事本程序的逻辑实现部分.本节的主要内容有以下3点: 1. 主窗口定义 -- 主要介绍记事本主界面窗口对应的窗口类及实现方案 2. RichEdi ...
- rhel安装eclipse
smb --> IDE --> 环境gcc(开发c) g++(开发c++)c++操作linux --> sqlite数据库linux平台自带sqlite数据库 基本SQL语言划分:D ...