Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:一开始用先求gcd再求lcm的方法一直WA,后来改用另外一种方法求lcm竟然过了,不明白.............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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator