【九度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. (转载)VB中ByVal与ByRef的区别

    ByVal是按值传送,在传的过程中不会改变原来的值,仅仅传送的是一个副本, 而 ByRef相反,从内存地址来说,后者是同一个内存地址. ByVal 与 ByRef(默认值)这两个是子过程的参数传递时, ...

  2. SonarQube的部分规则探讨

    引言:为了更好的使项目代码规范化,减少Bug的出现,因此最近引入了SonarQube来帮助检测代码问题,这里就分享部分有趣的规则. 注:因为保密原则,文章贴出来的代码都是我按照格式仿写的,并非公司源码 ...

  3. Redis | 第9章 Lua 脚本与排序《Redis设计与实现》

    目录 前言 1. Lua 脚本 1.1 Redis 创建并修改 Lua 环境的步骤 1.2 Lua 环境协作组件 1.3 EVAL 命令的实现 1.4 EVALSHA 命令的实现 1.5 脚本管理命令 ...

  4. Ubuntu apt代理apt-cacher-ng配置及使用

    apt-cacher-ng是更强大的apt代理服务器的替代方案,例如squid-deb-proxy.如果您正在运行小型家庭或办公室网络,那就别无所求.它可能缺少一些更高级的功能,但是可以立即进行配置, ...

  5. 【面试】【Linux】【Web】基础概念

    1. HTTP https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol 2. TCP handshake https://en.wikipe ...

  6. 实现android自动化测试部署与运行Shell脚本分享

    我的配置是linux 64, android4.2.2的sdk. 实现的细节都在代码注释里了,变量名以及echo的内容也是说明的一部分. 主流程为: 1.检测是否指定端口的模拟器已经运行,若有则关闭2 ...

  7. BigDecimal 中 关于RoundingMode介绍

    RoundingMode介绍 RoundingMode是一个枚举类,有以下几个常量:UP.DOWN.CEILING.FLOOR.HALF_UP.HALF_DOWN.HALF_EVEN.UNNECESS ...

  8. Django auth

    auth是django一个自带的用户验证系统,使用它可以减少我们的开发流程. 基本使用 大体流程: 自定义类 from django.contrib.auth.models import Abstra ...

  9. Redis哨兵 部署和配置

    目录 一.哨兵简介 哨兵介绍 哨兵原理 二.哨兵部署 环境介绍 哨兵配置 三.使用验证 一.哨兵简介 哨兵介绍 Sentinel(哨兵)是用于监控redis集群中Master状态的工具,其已经被集成在 ...

  10. 如何推翻JAVA的统治地位

    "java越来越过份了."php狠狠的说,他转头看着C:"C哥,您可是前辈,java最近砸了我不少场子,你老再不出来管管,我怕他眼里就没有您了啊." C哥吸烟, ...