2015-05-04 75 views
-1

我需要此名爲FlipSort的UVA問題的幫助。我使用O(nlogn)方法進行反演,測試用例很好。但我不知道爲什麼我仍然收到RuntimeError。這是我的Java代碼。UVA 10327 Java運行時錯誤

import java.io.*; 
import java.util.*; 
import java.math.*; 

class Main { 

    static int flips = 0; 

    public static void main(String... args) throws IOException { 
     InputStreamReader isr = new InputStreamReader(System.in); 
     BufferedReader in = new BufferedReader(isr); 
     String line; 
     while ((line = in.readLine()) != null) { 
      BigInteger[] numbers = getNumbers(in.readLine()); 
      String ans = getAns(numbers); 
      System.out.println(ans); 
     } 
    } 

    static String getAns(BigInteger[] numbers) { 
     flips = 0; 
     mergeSort(numbers); 
     return "Minimum exchange operations : " + flips; 
    } 

    static BigInteger[] mergeSort(BigInteger[] numbers) { 
     if (numbers.length <= 1) return numbers; 
     int n = numbers.length; 
     BigInteger[] p1 = mergeSort(subArray(numbers, 0, n/2)); 
     BigInteger[] p2 = mergeSort(subArray(numbers, n/2, n)); 
     return merge(p1, p2); 
    } 

    static BigInteger[] merge(BigInteger[] p1, BigInteger[] p2) { 
     BigInteger[] ans = new BigInteger[p1.length+p2.length]; 
     int i = 0, j = 0, k = 0; 
     while (i < p1.length || j < p2.length) { 
      if (i < p1.length && j < p2.length) { 
       if (p1[i].compareTo(p2[j]) <= 0) { 
        ans[k] = p1[i]; 
        i++; 
       } else { 
        flips++; 
        ans[k] = p2[j]; 
        j++; 
       } 
      } else if (i < p1.length) { 
       ans[k] = p1[i]; 
       i++; 
      } else if (j < p2.length) { 
       ans[k] = p2[j]; 
       j++; 
      } 
      k++; 
     } 
     return ans; 
    } 

    static BigInteger[] subArray(BigInteger[] numbers, int start, int end) { 
     BigInteger[] ans = new BigInteger[end-start]; 
     for (int i = start; i < end; i++) 
      ans[i-start] = numbers[i]; 
     return ans; 
    } 

    static BigInteger[] getNumbers(String line) { 
     String[] vals = line.split(" "); 
     BigInteger[] ans = new BigInteger[vals.length]; 
     for (int i = 0; i < ans.length; i++) 
      ans[i] = new BigInteger(vals[i]); 
     return ans; 
    } 
} 
+0

你能發佈錯誤嗎? –

+0

什麼是您的運行時錯誤? – Blip

+0

嗨!,我收到來自UVA在線裁判的錯誤,因此我無權查看更多詳細信息 –

回答

0

我讀的UVA 10327.使用說明指示

輸入將由EOF被終止。

我運行了你的程序,這裏是輸入'EOF'時的輸出。

3 
1 2 3 
Minimum exchange operations : 0 
EOF 

Exception in thread "main" java.lang.NumberFormatException: Zero length BigInteger 
    at java.math.BigInteger.<init>(BigInteger.java:296) 
    at java.math.BigInteger.<init>(BigInteger.java:476) 
    at so.UVA10327_FlipSort.getNumbers(UVA10327_FlipSort.java:73) 
    at so.UVA10327_FlipSort.main(UVA10327_FlipSort.java:17) 

UVA法官可以給你的程序提供EOF不同。所以我會用一個try-catch包裝主要的方法代碼,然後嘗試重新提交代碼給UVA判斷。

public static void main(String... args) throws IOException { 
    try { 
     InputStreamReader isr = new InputStreamReader(System.in); 
     BufferedReader in = new BufferedReader(isr); 
     String line; 
     while ((line = in.readLine()) != null) { 
      BigInteger[] numbers = getNumbers(in.readLine()); 
      String ans = getAns(numbers); 
      System.out.println(ans); 
     } 
    } catch (Throwable t) { 
     // Exit gracefully... 
     System.exit(0); 
    } 
}