用于计算两个数字的GCD和LCM的Java代码运行良好,但在其中一个在线平台上不被接受

用于计算两个数字的GCD和LCM的Java代码运行良好,但在其中一个在线平台上不被接受,java,greatest-common-divisor,lcm,Java,Greatest Common Divisor,Lcm,下面是我计算两个数字的gcd和lcm的代码。当我尝试不同的测试用例时,效果很好。但当我尝试在在线平台上提交时,它说的是错误的 package javapractice; import java.util.*; public class chef_gcdlcm { public static void main(String[] args) { try{ Scanner input = new Scanner(System.in);

下面是我计算两个数字的gcd和lcm的代码。当我尝试不同的测试用例时,效果很好。但当我尝试在在线平台上提交时,它说的是错误的

package javapractice;
import java.util.*;
public class chef_gcdlcm {

    public static void main(String[] args) {
        try{
            Scanner input = new Scanner(System.in);
            long t = input.nextInt();
            long l,s,temp;
            while(t-->0){
                long A = input.nextLong();
                long B = input.nextLong();
                temp = 1;
                if(A>B){ l = A;
                 s = B;}
                else{ l = B;
                s = A;              
                }
                while(temp!=0){
                    temp = l%s;
                    if(temp!=0){
                        if(temp>s){l = temp;}
                        else{s = temp;}
                    }   
                }
                long gcd = (A*B)/s;
                System.out.println(s +" "+gcd);
            }
            input.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

}

你的代码不起作用,这里是固定的

package javapractice;
import java.util.*;
public class chef_gcdlcm {

  public static void main(String[] args) {
    try{
        Scanner input = new Scanner(System.in);
        long t = input.nextInt();
        long l,s,temp;
        while(t-->0){
            long A = input.nextLong();
            long B = input.nextLong();
            temp = 1;
            if(A>B){ 
               l = A;
               s = B;
            }else{ l = B;
               s = A;              
            }
            while (s != 0) {
              temp = l % s;
              if (temp > s) {
                 l = temp;
              } else {
                 l = s;
                 s = temp;
              }
            }
           long gcd = (A * B) / l;
           System.out.println(l + ":" + gcd);
        }
        input.close();
    }catch(Exception e){
        e.printStackTrace();
    }

  }

}
你错过了什么

  • 当温度小于l时,您忘记将“大”设置为“小”(l=s)
  • 您不必检查此项:
    if(temp!=0)
    while循环会处理此问题
  • 你应该把A*B除以l,而不是最后除以s。s应该是0

  • 没有必要进行交换和比较。如有必要,将在第一次迭代后校正这些值

    Consider l = 24 and s = 36
    save = s;    save is 36
    l %= s;      l is still 24 
    s = l;       s is now 24
    l = save     l is now 36
    
    
    修改代码

    try {
        Scanner input = new Scanner(System.in);
        long t = input.nextInt();
        long l, s, temp;
        while (t-- > 0) {
            l = input.nextLong();
            s = input.nextLong();
            long A = s;
            long B = l;
            // doesn't matter which is larger.  It will be corrected
            // after the first iteration.
            while (s > 0) {
                 temp = s; 
                 l %= s; 
                 s = l;
                 l = temp;
            }
            long lcm =  (A / l) * B; // prevents overflow
            System.out.println(l + " " + lcm);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    
    注意:使用
    System.in
    创建扫描仪时,关闭扫描仪是没有必要的、有用的,而且通常是不好的做法。这是因为这也会关闭在程序期间不再可用的输入流。

    试试这个。