【九度OJ】题目1439:Least Common Multiple 解题报告
【九度OJ】题目1439:Least Common Multiple 解题报告
标签(空格分隔): 九度OJ
原题地址:http://ac.jobdu.com/problem.php?pid=1439
题目描述:
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 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.
输出:
For each problem instance, output a single line containing the corresponding LCM. All results will lie in the range of a 32-bit integer.
样例输入:
2
3 5 7 15
6 4 10296 936 1287 792 1
样例输出:
105
10296
Ways
BigInteger类好!
这个题的意思很简单,其实就是求指定数字的最小公倍数。
我们可以利用上一题的经验,求出m个数的共同最小公倍数即可。
做题时一个错误的地方就是注意两层循环的嵌套,把循环变量给写错了,导致一直出错。
import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String n = scanner.nextLine();
for (int i = 0; i < Integer.parseInt(n); i++) {
String line = scanner.nextLine();
String[] params = line.split(" ");
BigInteger a = new BigInteger(params[1]);
for (int j = 2; j < params.length; j++) {
BigInteger b = new BigInteger(params[j]);//是j,不是i
a = a.multiply(b).divide(a.gcd(b));
}
System.out.println(a.toString());
}
}
}
本来以为C++的版本也会同样的容易,可是还是遇到点问题,很不爽。原因是因为结果超出了int范围。改成long long就好了。
#include <stdio.h>
long long gcd(long long a, long long b) {
return b != 0 ? gcd(b, a % b) : a;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
while (n-- != 0) {
int m;
scanf("%d", &m);
long long answer = 1;
while (m-- != 0) {
int temp;
scanf("%d", &temp);
answer = answer * temp / gcd(answer, temp);
}
printf("%lld\n", answer);
}
}
return 0;
}
Date
2017 年 3 月 7 日
【九度OJ】题目1439:Least Common Multiple 解题报告的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- R语言与医学统计图形-【29】地图的绘制
R绘制地图原理: R使用一个个多边形(polygon)来表示每个区域,通过顺次连接GIS数据提供的每个区域多边形的坐标来逐点绘制这些多边形,所以理论上只要得到GIS数据就可绘制相应的地图. 地图绘制说 ...
- 巩固javaweb的第二十四天
巩固内容: 提示用户信息 在验证失败之后通常需要提示用户错误信息,可以通过下面的代码完成: alert("地址长度大于 50 位!"); 当使用 alert 提示错误信息时,参数是 ...
- 巩固javaweb第十六天
巩固内容: 下拉框 在注册功能中,地区的选择使用了下拉框,可以从地区选项中选择一个地区.在这个 例子中,只允许选择一个,而在有些情况下,下拉框可以进行多选.所以,从功能上来说, 下拉框具有单选按钮和复 ...
- 日常Java 2021/10/12
封装 在面向对象程式设计方法中,封装是指-种将抽象性函式接口的实现细节部分包装.隐藏起来的方法 封装可以被认为是一个保护屏障,防止该类的代码和数据被外部类定义的代码随机访问 要访问该类的代码和数据,必 ...
- webservice--常用注解
定义说明书的显示方法1.@WebService(serviceName="PojoService", portName="PojoPort", name=&qu ...
- mysql explain using filesort
创建表,字段tid上无索引(mysql 5.7) CREATE TABLE `test` ( `tid` int(11) DEFAULT NULL, `tname` varchar(12) DEFAU ...
- CentOS 7.3安装完整开发环境
系统版本CentOS 7.3(1611) 安装开发环境1) 通过group安装 yum groups mark install "Development Tools" yum gr ...
- Virtual Destructor
Deleting a derived class object using a pointer to a base class that has a non-virtual destructor re ...
- Handler与多线程
1.Handler介绍 在Android开发中,我们常会使用单独的线程来完成某些操作,比如用一个线程来完成从网络上下的图片,然后显示在一个ImageView上,在多线程操作时,Android中必须保证 ...
- 阿里云esc 安装 mysql5.7.27
1. 下载: wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm 2. 安装: (1) yum -y in ...