Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register
欢迎报名第四届北京大学游戏对抗邀请赛!

Re:一开始用先求gcd再求lcm的方法一直WA,后来改用另外一种方法求lcm竟然过了,不明白.............

Posted by harry at 2007-09-21 17:15:41 on Problem 2369
In Reply To:一开始用先求gcd再求lcm的方法一直WA,后来改用另外一种方法求lcm竟然过了,不明白............. Posted by:KosonLau at 2007-04-19 02:31:11
> 
> int gcd(int a,int b)
> {
> 	if(b==0)
> 		return a;
> 	else
> 		gcd(b,a%b);
> }
> long lcm1(int a,int b)          //用这个一直WA
> {
> 	if(a==b)
> 		return a;
> 	else
> 		return a*b/gcd(a,b);  // 这里a*b超出了 int的范围
> }
> 
> 改用这个就AC了..
> int lcm2(int a,int b)
> {
> 	int temp;
> 	if(a==b)
> 		return a;
> 	if(a<b)
> 	{
> 		temp=a;
> 		a=b;
> 		b=temp;
> 	}
> 	for(int i=1;;i++)
> 	{
> 		int j;
> 		j=i*a;
> 		if(j%b==0)
> 			return j;
> 	}
> 
> 
> }

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator