Java 用于计算两个数字的 GCD 和 LCM 的代码工作正常,但未在其中一个在线平台上被接受
Java code for calculating GCD and LCM of two numbers is working fine but not being accepted on one of the online platforms
下面是我计算两个数的 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
创建扫描仪时,关闭扫描仪没有必要,也没有用,通常也是不好的做法。这是因为这也会关闭在程序运行期间不再可用的输入流。
下面是我计算两个数的 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
创建扫描仪时,关闭扫描仪没有必要,也没有用,通常也是不好的做法。这是因为这也会关闭在程序运行期间不再可用的输入流。