Test instructions: Least common multiple of multiple numbers
It's simple, but I started off by estimating that the results would go out of bounds (the maximum value of int)
ImportJava.util.*;ImportJava.io.*; Public classmain{ Public Static voidMain (string[] arg) {Scanner Scan=NewScanner (NewBufferedinputstream (system.in)); intn =Scan.nextint (); int[] Nums =New int[1001]; while(N--! =0){ intm =Scan.nextint (); for(inti = 0; I! = m; i + +) {Nums[i]=Scan.nextint (); } intLCM = 1; for(inti = 0; I! = m; i + +) {LCM=GETLCM (Lcm,nums[i]); } System.out.println (LCM); } scan.close (); } Static intGETLCM (intAintb) {//least common multiple LCM is the product divided by greatest common divisor gcd returnb*a/GETGCD (A, b); } Static intGETGCD (intAintb) {//seek greatest common divisor, and divide the method while(b%a!=0){ inttemp = b%A; b=A; A=temp; } returnA; };}
Therefore, I changed the GETLCM (int a,int b) method to avoid cross-border situations, and the result is of course Accepted
Static int GETLCM (int A,int b) {// least common multiple LCM is product divided by greatest common divisor gcd b=b/ GETGCD (A, b); return b*A;
}
Personal feeling, this algorithm can be re-optimized, it should be ~
hdoj-acm1019 (JAVA) Number of least common multiple