在Java代码中获取NZEC

在Java代码中获取NZEC,java,runtime-error,Java,Runtime Error,我正在努力解决这个问题 我在codechef上得到运行时错误NZEC。我在互联网上搜索,但没有成功地使我的代码 这是我的代码: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main { public BigInteger gcd(BigInteger a,Big

我正在努力解决这个问题

我在codechef上得到运行时错误NZEC。我在互联网上搜索,但没有成功地使我的代码

这是我的代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {

    public BigInteger gcd(BigInteger a,BigInteger b){
        if(b.compareTo(BigInteger.valueOf(0)) == 0)
            return a;
        return gcd(b,a.mod(b));
    }

    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        int t = 1;
        Main obj = new Main();
        try{
            str = br.readLine();
            t = Integer.parseInt(str);
        }
        catch(IOException e){
            System.out.println("error");
        }

        for(int w = 0; w < t; w++){
            BigInteger a = BigInteger.valueOf(1);
            BigInteger b = BigInteger.valueOf(1);
            BigInteger c = BigInteger.valueOf(1);
            BigInteger d = BigInteger.valueOf(1);
            BigInteger k = BigInteger.valueOf(1);
            try{
                str = br.readLine();
                String s[] = str.split(" ");
                a = new BigInteger(s[0]);
                b = new BigInteger(s[1]);
                c = new BigInteger(s[2]);
                d = new BigInteger(s[3]);
                k = new BigInteger(s[4]);
            }
            catch(IOException e){
                System.out.println("error");
            }

            BigInteger g1,g2,num;
            if(a.compareTo(b) < 0){
                num = a;
                a = b;
                b = num;
            }

            if(c.compareTo(d) < 0){
                num = c;
                c = d;
                d = num;
            }

            g1 = obj.gcd(a,b);
            g2 = obj.gcd(c,d);

            if(g1.compareTo(g2) < 0){
                num = g1;
                g1 = g2;
                g2 = num;
            }
            BigInteger g3 = obj.gcd(g1,g2);

            BigInteger l = g1.divide(g3);
            l = l.multiply(g2);

            BigInteger res = k.divide(l);
            BigInteger fin = res.multiply(BigInteger.valueOf(2));
            fin = fin.add(BigInteger.valueOf(1));
            System.out.println(fin);
        }
    }

}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.math.biginger;
公共班机{
公共BigInteger gcd(BigInteger a、BigInteger b){
如果(b.compareTo(BigInteger.valueOf(0))==0)
返回a;
返回gcd(b,a.mod(b));
}
公共静态void main(字符串[]args){
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
字符串str;
int t=1;
Main obj=新的Main();
试一试{
str=br.readLine();
t=Integer.parseInt(str);
}
捕获(IOE异常){
System.out.println(“错误”);
}
对于(int w=0;w

有人能告诉我哪里做错了吗?

除非出现不太可能的情况,否则我所看到的通过该程序获得非零退出代码的唯一可能性是

  • 除以零,但如果输入符合规范,就不会发生这种情况
  • 意外的输入格式导致出现
    NumberFormatException
因此,我正在研究后一种假设。一个简单的检查方法是将两个
catch(IOException e)
替换为
catch(Exception e)
,如果输入的格式确实不是您期望的格式,那么您将得到一个“错误答案”(但在更改后得到一个WA并不能证明假设是正确的)

第一行上的任何额外空格都会导致
Integer.parseInt(str)
中出现
NumberFormatException
。任何后续行上的附加空格都会导致由
str.split(“”
创建的
String[]
包含五个以上的元素,如果在该行的第五个数字之前出现任何此类元素,程序将尝试创建
biginger.valueOf(“”
),这再次导致
NumberFormatException
。因此,我建议使用一种更健壮的输入方法,例如
java.util.Scanner
,它可以毫无问题地处理额外的空白。这里使用的方法是
nextInt()
用于测试用例的数量,而
nextLong()
用于其他测试用例

Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
for(int w = 0; w < t; w++){
    BigInteger a = BigInteger.valueOf(scn.nextLong());
    BigInteger b = BigInteger.valueOf(scn.nextLong());
    BigInteger c = BigInteger.valueOf(scn.nextLong());
    BigInteger d = BigInteger.valueOf(scn.nextLong());
    BigInteger k = BigInteger.valueOf(scn.nextLong());

    BigInteger g1,g2,num;
    ...
该方法应该是静态的。它不涉及任何状态,因此必须创建一个对象来调用它是不好的

另一点,与问题中可能出现的小数字不相关,但与处理大数字相关的是,您使其递归。Java通常(如果有的话)不执行尾部调用优化,并且调用堆栈通常只能处理几千个调用,因此使用递归实现可能会出现
StackOverflowerError
。(但由于欧几里德算法的调用深度是对数的,因此只涉及较大的数字。)

捕获异常只是为了将“错误”打印到标准输出是错误的。如果你不能做一些更有意义的事情来处理它,那就不要抓住它

for(int w = 0; w < t; w++){
    BigInteger a = BigInteger.valueOf(1);
    BigInteger b = BigInteger.valueOf(1);
    BigInteger c = BigInteger.valueOf(1);
    BigInteger d = BigInteger.valueOf(1);
    BigInteger k = BigInteger.valueOf(1);
无意义的
再次捕获

    if(a.compareTo(b) < 0){
        num = a;
        a = b;
        b = num;
    }

    if(c.compareTo(d) < 0){
        num = c;
        c = d;
        d = num;
    }
if(a.与(b)相比<0){
num=a;
a=b;
b=num;
}
如果(c)与(d)相比<0{
num=c;
c=d;
d=num;
}

我怀疑您交换是为了避免红利小于除数的
mod
操作。在某些地方,这种微观优化非常重要,但这不是其中之一。如果你有理由关心像这样的小事,你会有更多的事情要做。例如,可以使用
long
解决手头的问题(有一个地方需要调整算法以避免可能的溢出),该原语类型的更快的算法将使您从这里的交换中获得的小收益相形见绌。

尝试使用BufferedReader而不是Scanner类,而不是使用Try-catch,使用throws NumberFormatException和throws IOException。这是因为如果你使用try-catch,你可能会得到错误的答案

int takingInput = sc.nextInt();
因此,我在这一行代码中得到了NZEC错误,但为了避免此运行时错误,您必须验证扫描仪是否包含有效的整数输入值,因此请尝试用以下代码替换上述代码行:

int takingInput=0; 
if(sc.hasNextInt()){
takingInput= sc.nextInt();
}  

这对我有用

NZEC表示“非零退出代码”。它本质上是说你的程序遇到了
    if(a.compareTo(b) < 0){
        num = a;
        a = b;
        b = num;
    }

    if(c.compareTo(d) < 0){
        num = c;
        c = d;
        d = num;
    }
int takingInput = sc.nextInt();
int takingInput=0; 
if(sc.hasNextInt()){
takingInput= sc.nextInt();
}  
import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(system.in);
        int cases = sc.nextInt();
    }
}
try
{
  // code which you think might throw an exception or whole main class code
}
catch(Exception t){
// you got the exception. 
}
int x;
if(sc.hasNextInt()) 
    x=sc.nextInt();