在代码ENTEXAM无法识别的错误NZEC

问题描述:

我工作的一个问题上codechef.com https://www.codechef.com/problems/ENTEXAM在代码ENTEXAM无法识别的错误NZEC

这里是我的problem-

import java.io.*; 
class Entrance_Final 
{ 
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); 
    static int test_case=0;//Test cases 
    static int students=0; 
    static int qualifiers=0; 
    static long result=0; 
    static int exams=0; 
    static long max_marks=0; 
    static long[]sigma_res; 
    static long sergey_score=0; 
public static void main(String[]args)throws IOException 
{ 
    try 
    { 
     //System.out.println("Enter number of test cases."); 
     test_case=Integer.parseInt(in.readLine()); 
     for(int lv=1;lv<=test_case;lv++) 
      comp_min_marks(); 
    } 
    catch(Exception e) 
    { 
     System.err.println(e); 
    } 
} 

public static void comp_min_marks()throws IOException 
{ 
    try 
    { 
     //System.out.println("Enter students,enrollees,exams and maximum marks."); 
     String a=in.readLine(); 
     a=a.trim(); 
     int flag=0; 
     int times=1; 
     for(int lv=0;lv<a.length();lv++) 
     { 
      if(a.charAt(lv)==' '&&(times==1)) 
      { 
       students=Integer.parseInt(a.substring(0,lv)); 
       flag=lv+1; 
       times++; 
      } 
      else if(a.charAt(lv)==' '&&(times==2)) 
      { 
       qualifiers=Integer.parseInt(a.substring(flag,lv)); 
       flag=lv+1; 
       times++; 
      } 
      else if(a.charAt(lv)==' '&&(times==3)) 
      { 
       exams=Integer.parseInt(a.substring(flag,lv)); 
       flag=lv+1; 
       times++; 
       max_marks=Long.parseLong(a.substring(flag)); 
       break; 
      } 
     } 
     sigma_res=new long[students-1]; 
     //System.out.println("Enter the marks of all the students during their exams,each ones in one line"); 
     for(int lv=0;lv<students-1;lv++) 
     { 
      String b=in.readLine(); 
      sigma_res[lv]=int_sum(b); 
     } 
     //System.out.println("Now enter Sergey's scores"); 
     if(exams==1) 
     { 
      //String b=in.readLine(); 
      sergey_score=0; 
     } 
     else 
     { 
      String b=in.readLine(); 
      sergey_score=int_sum(b); 
     } 
     sigma_res=doQuickSort(0,students-2); 
     result=sigma_res[students-qualifiers-1]-sergey_score+1; 
     if(result<0) 
      System.out.println("0"); 
     else if(result<=max_marks) 
      System.out.println(result); 
     else 
      System.out.println("Impossible"); 
    } 
    catch(Exception e) 
    { 
     System.err.println(e); 
    } 
} 

public static long int_sum(String b)throws IOException 
{ 
    try 
    { 
     b=b.trim(); 
     long res=0; 
     int flag=0; 
     for(int lv=0;lv<b.length();lv++) 
     { 
      if(b.charAt(lv)==' ') 
      { 
       res+=Long.parseLong(b.substring(flag,lv)); 
       flag=lv+1; 
      } 
     } 
     res+=Long.parseLong(b.substring(flag)); 
     return res; 
    } 
    catch(Exception e) 
    { 
     System.err.println(e); 
     return -1; 
    } 
} 

private static long[] doQuickSort(int low,int high)throws IOException 
{ 
    try 
    { 

     if(high-low<1) 
      return sigma_res; 

     int wall=low; 
     int pivot_pos=(int)(Math.random()*(high-low))+low; 
     long pivot=sigma_res[pivot_pos]; 
     long temp=sigma_res[high]; 
     sigma_res[high]=pivot; 
     sigma_res[pivot_pos]=temp; 
     pivot_pos=high; 
     for(int lv=low;lv<=high-1;lv++) 
     { 
      if(pivot>sigma_res[lv]) 
      { 
       temp=sigma_res[lv]; 
       sigma_res[lv]=sigma_res[wall]; 
       sigma_res[wall]=temp; 
       wall++; 
      } 
     } 
     temp=sigma_res[wall]; 
     sigma_res[wall]=pivot; 
     sigma_res[pivot_pos]=temp; 
     pivot_pos=wall;  
     doQuickSort(low,wall-1); 
     doQuickSort(wall+1,high); 
     return sigma_res; 
    } 
    catch(Exception e) 
    { 
     System.err.println(e); 
     return sigma_res; 
    } 
} 

}

当你有解决方案可能会注意到,我已经在我的程序中包含了相当多的try-catch块来返回任意异常的所有代码。这是因为我的代码总是收到NZEC-Error(当我在线提交时),尽管使用这些代码块,错误仍然存​​在。我一再看过这个问题的限制,但没有搞清楚问题是什么。

P.S我无权访问此问题的测试用例。

+0

发布错误的堆栈跟踪 – rafid059

既然这里没有回应,而且我能弄清楚这个问题,我想我可以回答我的问题。所以首先有没有例外可言,而是我得到一个的VirtualMachineError在该行

sigma_res=new long[students-1]; 

(不知道为什么这是怎么回事,我所知道的是“学生”的价值超过其约束定义限制)。

看过几个解决方案之后,我发现这个问题是由于将println语句从main()方法中取出并放入另一个方法引起的。

当我把println语句放回main()时,解决方案就被接受了。

P.S我仍然不知道为什么程序不会终止,当println语句在另一个方法所在的地方。