杭电 acm 1019 wrong answer
原题:http://acm.hdu.edu.cn/showproblem.php?pid=1019ProblemDescriptionTheleastcommonmult...
原题:http://acm.hdu.edu.cn/showproblem.php?pid=1019
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 "stdio.h"
__int64 s[1000];
int hcf(int a,int b)
{
int r=0;
while(b!=0)
{
r=a%b;
a=b;
b=r;
}
return(a);
}
int lcd(int u,int v,int h)
{
return(u*v/h);
}
int main(int argc, char* argv[])
{
int n,m,i;
while(scanf("%d",&n)==1)
{
while(n--)
{
scanf("%d",&m);
for(i=0;i<=m-1;i++)
scanf("%I64d",&s[i]);
for(i=0;i<=m-2;i++)
{
s[i+1]=lcd(s[i],s[i+1],hcf(s[i],s[i+1]));
}
printf("%I64d\n",s[m-1]);
}
}
return 0;
}
测试数据都过了,可是还是wrong answer,郁闷啊,望高手解救。
贴代码就不必了,告诉我哪里错了就好,最好详细一点。。 展开
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 "stdio.h"
__int64 s[1000];
int hcf(int a,int b)
{
int r=0;
while(b!=0)
{
r=a%b;
a=b;
b=r;
}
return(a);
}
int lcd(int u,int v,int h)
{
return(u*v/h);
}
int main(int argc, char* argv[])
{
int n,m,i;
while(scanf("%d",&n)==1)
{
while(n--)
{
scanf("%d",&m);
for(i=0;i<=m-1;i++)
scanf("%I64d",&s[i]);
for(i=0;i<=m-2;i++)
{
s[i+1]=lcd(s[i],s[i+1],hcf(s[i],s[i+1]));
}
printf("%I64d\n",s[m-1]);
}
}
return 0;
}
测试数据都过了,可是还是wrong answer,郁闷啊,望高手解救。
贴代码就不必了,告诉我哪里错了就好,最好详细一点。。 展开
展开全部
这个题目说了 数字会达到32位,用64位也不会错,但你只在定义时用了 __int64 位,在函数定义里也要把参数类型改为__int64 型,否则怎么能匹配呢 ?
下面的代码只将参数里的类型全部替换成__int64 就AC了。
#include "stdio.h"
__int64 s[1000];
__int64 hcf(__int64 a,__int64 b)
{
__int64 r=0;
while(b!=0)
{
r=a%b;
a=b;
b=r;
}
return(a);
}
__int64 lcd(__int64 u,__int64 v,__int64 h)
{
return(u*v/h);
}
int main(int argc, char* argv[])
{
int n,m,i;
while(scanf("%d",&n)==1)
{
while(n--)
{
scanf("%d",&m);
for(i=0;i<=m-1;i++)
scanf("%I64d",&s[i]);
for(i=0;i<=m-2;i++)
{
s[i+1]=lcd(s[i],s[i+1],hcf(s[i],s[i+1]));
}
printf("%I64d\n",s[m-1]);
}
}
return 0;
}
下面的代码只将参数里的类型全部替换成__int64 就AC了。
#include "stdio.h"
__int64 s[1000];
__int64 hcf(__int64 a,__int64 b)
{
__int64 r=0;
while(b!=0)
{
r=a%b;
a=b;
b=r;
}
return(a);
}
__int64 lcd(__int64 u,__int64 v,__int64 h)
{
return(u*v/h);
}
int main(int argc, char* argv[])
{
int n,m,i;
while(scanf("%d",&n)==1)
{
while(n--)
{
scanf("%d",&m);
for(i=0;i<=m-1;i++)
scanf("%I64d",&s[i]);
for(i=0;i<=m-2;i++)
{
s[i+1]=lcd(s[i],s[i+1],hcf(s[i],s[i+1]));
}
printf("%I64d\n",s[m-1]);
}
}
return 0;
}
展开全部
O(∩_∩)O哈哈~ 我也是个ACMer 这题说了数据保证在32位int以内,所以不必使用__int64;注意到没有求最小公倍数的那个函数里面是:return(u*v/h); 应该改为return(u/h*v); 这样就保证乘法运算不会溢出。至于你使用了__int64为什么还是WA呢,还不是很清楚,我再看看。。。
刚仔细看看题目,貌似题目中的m没有限定范围啊,要是m 是1005怎么办?
还是不要用数组保存的好,来一个计算一个吧。你仔细看看。。。
刚仔细看看题目,貌似题目中的m没有限定范围啊,要是m 是1005怎么办?
还是不要用数组保存的好,来一个计算一个吧。你仔细看看。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询