【九度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 解题报告的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. MYSQL5.8-----5

  2. Oracle-除了会排序,你对ORDER BY的用法可能一无所知!

    导读 为什么只有ORDER BY后面可以使用列别名 为什么不推荐使用ORDER BY后接数字来排序 为什么视图和子查询里面不能使用ORDER BY -- ​小伙伴们在进行SQL排序时,都能很自然的使用 ...

  3. C语言中储存的大小端问题

    一.大小端定义 研究变量的高低字节:从左往右看,字节序递增,也就是最右边是最低字节,最右边是最高字节.如 int i = 0x01020304, 01是高字节,04是低字节.如果是字符串如char a ...

  4. LeetCode子矩形查询

    LeetCode 子矩形查询 题目描述 请你实现一个类SubrectangleQueries,它的构造函数的参数是一个rows * cols的矩形(这里用整数矩阵表示),并支持以下两种操作: upda ...

  5. Java【常用的日期操作】

    目录 1.设置时间 2.获取年月日时分秒 3.通过运算获取时间 4.和Date类转换 5.格式化时间 6.新功能LocalDate:当前日期格式化 7.示例 java.util.Calendar 类是 ...

  6. LeetCode382-链表随机节点

    原题链接:[382. 链表随机节点]:https://leetcode-cn.com/problems/linked-list-random-node/ 题目描述: 给定一个单链表,随机选择链表的一个 ...

  7. C++自定义字符串类

    //header.h #ifndef _HEADER_H #define _HEADER_H #define defaultSize 128 #include<iostream> #inc ...

  8. Docker学习(一)——安装docker

    Suse12上安装docker   对于suse13.2之后的版本,因为docker已经被添加到了suse仓库中,直接使用sudo zypper install docker即可.   suse12不 ...

  9. 【Linux】【Commands】基础概念及常用基础命令

    命令的语法通用格式: ------------------------------------------------ #COMMAND OPTIONS ARGUMENTS 发起命令:请求内核将某个二 ...

  10. Mockito 简介

    Mockito 是一种 Java Mock 框架,主要是用来做 Mock 测试,它可以模拟任何 Spring 管理的 Bean.模拟方法的返回值.模拟抛出异常等等,在了解 Mockito 的具体用法之 ...