| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1239 人关注过本帖
标题:求多个数的最小公倍数
只看楼主 加入收藏
狂人老大
Rank: 1
来 自:杭州
等 级:新手上路
威 望:1
帖 子:394
专家分:0
注 册:2007-6-21
收藏
 问题点数:0 回复次数:6 
求多个数的最小公倍数

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



我的写的程序代码是:
#include <iostream>
using namespace std;
int gcd(int,int);
int lcm(int,int);
int main()
{
int n,k;
while(cin>>k)
for(int m=0;m<k,cin>>n;m++)
{
int m[100]={0};
int LCM;
for(int i=0;i<n;i++)
cin>>m[i];
if(n==1)
LCM=m[0];
else
{
LCM=lcm(m[0],m[1]);
for(int j=2;j<n;j++)
LCM=lcm(LCM,m[j]);
}
cout<<LCM<<endl;
}
return 0;
}
int gcd(int m,int n)
{
if(m%n==0)
return n;
else
return gcd(n,m%n);
}
int lcm(int a,int b)
{
return (a*b)/gcd(a,b);
}


这个程序就是通不过,各位看看,给点意见

搜索更多相关主题的帖子: 最小公倍数 LCM The will integer 
2007-11-02 22:59
雨中飞燕
Rank: 3Rank: 3
等 级:禁止访问
威 望:8
帖 子:2200
专家分:0
注 册:2007-8-9
收藏
得分:0 
很简单的溢出



by 雨中飞燕 C/C++学习讨论群:46520219
[url=http://yzfy.org/]C/C++算法习题(OnlineJudge)论坛:[/url] http://yzfy.org/
Blog: http://yzfy.programfan.com

[url=http://bbs.bc-cn.net/viewthread.php?tid=163571]请大家不要用TC来学习C语言,点击此处查看原因[/url] [url=http://blog.programfan.com/article.asp?id=24801]请不要写出非int声明的main函数[/url]
[url=http://bbs.bc-cn.net/viewthread.php?tid=181314]C++编写的Windows界面游戏[/url]
2007-11-02 23:17
狂人老大
Rank: 1
来 自:杭州
等 级:新手上路
威 望:1
帖 子:394
专家分:0
注 册:2007-6-21
收藏
得分:0 
那应该怎样改呢?
想了好久不知道啊

ACMer的QQ群:33741351
单片机QQ群:55130117
2007-11-02 23:23
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 

题目有说个数<100吗?你为什么自说自话定义int m[100]={0};
另外,虽然题目说All results will lie in the range of a 32-bit integer.
return (a*b)/gcd(a,b);这句仍然是危险的。小朋友还是要积累经验啊

2007-11-02 23:44
狂人老大
Rank: 1
来 自:杭州
等 级:新手上路
威 望:1
帖 子:394
专家分:0
注 册:2007-6-21
收藏
得分:0 
我自己也不知道把这个int m[100]={0};中的100定义多大
还有就是return (a*b)/gcd(a,b);这句危险在哪呢?指教下

ACMer的QQ群:33741351
单片机QQ群:55130117
2007-11-03 18:03
狂人老大
Rank: 1
来 自:杭州
等 级:新手上路
威 望:1
帖 子:394
专家分:0
注 册:2007-6-21
收藏
得分:0 
大家救救我吧    这个程序怎样才能不溢出呢

ACMer的QQ群:33741351
单片机QQ群:55130117
2007-11-03 20:48
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
以下是引用狂人老大在2007-11-3 18:03:34的发言:
我自己也不知道把这个int m[100]={0};中的100定义多大
还有就是return (a*b)/gcd(a,b);这句危险在哪呢?指教下

没说就定义最大可能数

a*b会溢出,因为只保证到a,b分别为32bit


Fight  to win  or  die...
2007-11-03 20:58
快速回复:求多个数的最小公倍数
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018952 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved