(杭电1019 最小公倍数) Least Common Multiple
Least Common Multiple
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 64855 Accepted Submission(s): 24737
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
这道水题连续提交五次,第六次才AC。。。。。。(我真是菜,最近做做的题有点自闭)
这是是我多次修改仍然不AC的题解(测试样例全过自己测了几个也没问题,很绝望)
#include <stdio.h>
#include <math.h>
int main() {
int t,temp;
scanf("%d",&t);
for(int i=1; i <= t; i++) {
int n;
scanf("%d",&n);
for(int j=1; j <= n ; j++) {
int num;
scanf("%d",&num);
if(j == 1) {
temp=num;
continue;
}
int max;
if(temp > num)
max=num;
else
max=temp;
for( ; max >= 1; max--)
if(temp%max == 0&&num%max == 0)
break;
temp=temp*num/max;
}
printf("%d\n",temp);
}
return 0;
}
最后有dalao指点,把求公因数的方法改成辗转相乘法并用函数嵌套终于AC 无奈绝望╮(╯﹏╰)╭
关于辗转相除法求最大公因数,举个栗子:
a=6, b=4; 第一步 a=a%b=6%4=2;
第二步 b=4;
第三步 a%b=2%4=0,此时a == 0,b=(上一步)a;
此时b就是最大公因数;
(算了上详细讲解链接 https://www.cnblogs.com/shine-yr/p/5216966.html )
以下为正确代码
#include <stdio.h>
#include <math.h>
int gcd(int a,int b){ //辗转相除法
if(b == 0)
return a;
return gcd(b,a%b);
}
int lcm(int a,int b)
{
return a/gcd(a,b)*b; //把函数打包
}
int main() {
int t,temp,n;
scanf("%d",&t);
for(int i=1; i <= t; i++) {
scanf("%d",&n);
int temp = 1;
for(int j=1; j <= n ; j++) {
int num;
scanf("%d",&num);
temp = lcm(temp,num);
}
printf("%d\n",temp);
}
return 0;
}
(杭电1019 最小公倍数) Least Common Multiple的更多相关文章
- 杭电1019 Least Common Multiple【求最小公倍数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1019 解题思路:lcm(a,b)=a*b/gcd(a,b) 反思:最开始提交的时候WA,以为是溢出了, ...
- 26最小公倍数 lowest common multiple
题目描述 正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数. 输入描述:输入两个正整数A和B. 输出描述:输出A和B的最小公倍数. 输入例子 ...
- 杭电OJ 输入输出练习汇总
主题 Calculate a + b 杭电OJ-1000 Input Each line will contain two integers A and B. Process to end of fi ...
- 杭电1019Least Common Multiple
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1019 题目: Problem Description The least common multiple ...
- HDOJ 1019 Least Common Multiple(最小公倍数问题)
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
- 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 ...
- Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】
Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...
- hdu 2028 Lowest Common Multiple Plus(最小公倍数)
Lowest Common Multiple Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- ACM hdu 1019 Least Common Multiple
Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...
随机推荐
- pip 升级 Appium-Python-Client
第一种方法: pip install --upgrade Appium-Python-Client 如果出现权限提醒: sudo -H pip install --upgrade Appium-Pyt ...
- SQL Server用户自定义函数(UDF)
一.UDF的定义 和存储过程很相似,用户自定义函数也是一组有序的T-SQL语句,UDF被预先优化和编译并且可以作为一个单元来进行调用. UDF和存储过程的主要区别在于返回结果的方式: 使用UDF时可传 ...
- 无法执行程序。所执行的命令为 "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\csc.exe" /noconfig /fullpaths @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\b411ea32\b48a9fb\aun5r0xd.c
解决方案 将应用程序池进程模型中的标识设为“LocalSystem”即可.
- 51nod 1967路径定向(dfs、欧拉回路)
1967 路径定向 基准时间限制:1.2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 给出一个有向图,要求给每条边重定向,使得定向后出度等于入度的点最多,输出答案和任意一种方案 ...
- 【3】【MOOC】Python游戏开发入门-北京理工大学【第三部分-游戏开发之机制(事件处理机制)】
学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...
- DQL、DML、DDL、DCL概念与区别
SQL(Structure Query Language)语言是数据库的核心语言. SQL的发展是从1974年开始的,其发展过程如下:1974年-----由Boyce和Chamberlin提出,当时称 ...
- cpu和gpu关于图像的分工
cpu: 图像IO导入: 图像生成: 部分图片解码: gpu: 几何图像表示: 几何图像组合等处理: 部分格式图像解码: 图像的光学.几何学操控:
- 108.UIView关于布局和约束的方法(AutoLayout)
http://blog.csdn.net/wangyanchang21/article/details/52270136 关于布局(UIViewHierarchy) 1.layoutSubviews ...
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
转自:http://blog.csdn.net/hwj3747/article/details/53635539 在Java使用HttpURLConnection请求rest接口的时候出现了POST请 ...
- [HEOI2012]朋友圈
题目 我们发现我们要求的是一个最大团问题,众所周知这是一个\(NP\)难问题,除了爆搜没有什么别的方法,但是这道题我们可以根据图的特殊性质入手 我们如果把\(B\)国的人分成奇数和偶数两类,就会发现奇 ...