杭电ACM1019题求教 为什么老是wrong answer?? 明明答案是对的啊。。
求最小公倍数InputInputwillconsistofmultipleprobleminstances.Thefirstlineoftheinputwillconta...
求最小公倍数
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.
我的代码是
#include<stdio.h>
int LCM(int a,int b)
{
int t,s;
s=a*b;
if(a<b)
{
t=a;
a=b;
b=t;
}
while(a%b!=0)
{
t=b;
b=a%b;
a=t;
}
return s/b;
}
void main ()
{
int a,b,n1,n2;
scanf("%d",&n1);
while(n1--)
{
scanf("%d",&n2);
scanf("%d",&a);
n2--;
while(n2--)
{
scanf("%d",&b);
a=LCM(a,b);
}
printf("%d\n",a);
}
} 展开
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.
我的代码是
#include<stdio.h>
int LCM(int a,int b)
{
int t,s;
s=a*b;
if(a<b)
{
t=a;
a=b;
b=t;
}
while(a%b!=0)
{
t=b;
b=a%b;
a=t;
}
return s/b;
}
void main ()
{
int a,b,n1,n2;
scanf("%d",&n1);
while(n1--)
{
scanf("%d",&n2);
scanf("%d",&a);
n2--;
while(n2--)
{
scanf("%d",&b);
a=LCM(a,b);
}
printf("%d\n",a);
}
} 展开
展开全部
All integers will be positive and lie within the range of a 32-bit
integer.
说明了a和b都是32位的数据
int LCM(int a,int b)
{
int t,s; //s的最大值只可能是(2^31) - 1
s=a*b; //这里可能会溢出,
//因为如果a和b都是 (2^31) - 1这个数值,那么他们两个相乘的话,
//结果会大于2^31 - 1, 一定会溢出, 溢出意味着出错
if(a<b)
{
t=a;
a=b;
b=t;
}
ps:若有不明白的地方,可以追问
integer.
说明了a和b都是32位的数据
int LCM(int a,int b)
{
int t,s; //s的最大值只可能是(2^31) - 1
s=a*b; //这里可能会溢出,
//因为如果a和b都是 (2^31) - 1这个数值,那么他们两个相乘的话,
//结果会大于2^31 - 1, 一定会溢出, 溢出意味着出错
if(a<b)
{
t=a;
a=b;
b=t;
}
ps:若有不明白的地方,可以追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询