2016-12-05 42 views
0

我工作的一個問題上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

回答

0

既然這裏沒有迴應,而且我能弄清楚這個問題,我想我可以回答我的問題。所以首先有沒有例外可言,而是我得到一個的VirtualMachineError在該行

sigma_res=new long[students-1]; 

(不知道爲什麼這是怎麼回事,我所知道的是「學生」的價值超過其約束定義限制)。

看過幾個解決方案之後,我發現這個問題是由於將println語句從main()方法中取出並放入另一個方法引起的。

當我把println語句放回main()時,解決方案就被接受了。

P.S我仍然不知道爲什麼程序不會終止,當println語句在另一個方法所在的地方。