Runtime error in UVA Online Judge [closed]

£可爱£侵袭症+ 提交于 2019-12-24 04:45:12

问题


I'm doing problem 10003 "cutting sticks" of the UVa online judge, I'm pretty sure my code works and I think I' correctly exiting the problem. But still I get a runtime error, I read somewhere that is because I'm not exiting the application like I'm supposed to do it. I wish you guys can help me with this problem.

import java.io.*;
class MAIN {

    static int len, c, min;
    static int mat[][] = new int[52][52];
    static int arr[] = new int [52];
    static BufferedReader BufferReader = new BufferedReader(new InputStreamReader(System.in));
    public static void llenaarr()throws IOException{
            for(int i=0; i<c-1; i++) {
                    arr[i+1] = Integer.parseInt(BufferReader.readLine());
                    }
            arr[0] = 0;
            arr[c] = len;
            }
    public static void llenamat(){
            for(int i=0; i<=c; i++) {
                    for(int j=0;j<=c;j++) {
                            mat[i][j] = Integer.MAX_VALUE;
                            }
                    }
            for(int i=0; i<c; i++) {
                    mat[i][i] = 0;
                    mat[i][i+1] = 0;
                    }
            mat[c][c] = 0;
            for(int i=0; i<c-1; i++) {
                    mat[i][i+2] = arr[i+2] - arr[i];
                    }
            }
    public static void minimo(){
            for(int k=3; k<=c; k++) {
                    for(int i=0; i<=c-k; i++) {
                            for(int j=i+1; j<=i+k-1; j++) {
                                    min=mat[i][j] + mat[j][i+k] + arr[i+k] - arr[i];
                                    if((min< mat[i][i+k])) {
                                            mat[i][i+k] = min;
                                            }
                                    }
                            }
                    }
            }

    public static void main (String args[]) throws IOException {
            while((len = Integer.parseInt(BufferReader.readLine())) > 0){
                    c = (Integer.parseInt(BufferReader.readLine()))+1;

                    llenaarr();
                    llenamat();
                    minimo();
                    System.out.println("The minimum cutting is "+mat[0][c]+".");
                    }
            }
    }

回答1:


Your program is throwing some kind of runtime Exception during processing input. To verify this, you should encapsulate your code (the part that operates on the input and produces output) in a try-catch block.

The first thing to try to catch is the generic Exception. This will catch any type of runtime exception. Run it through the online judge again, and you should now see a Wrong Answer result (assuming something is not processing right, because it is throwing an exception after all).

Now comes the detective work. Go back to your source code and look at all the calls you make and use the API Docs to check all the possible exceptions they may throw. For example, if you call Integer.parseInt(inputString), it may be throwing a NumberFormatException. So above your catch (Exception e) block, start adding some more specific exceptions you think may be causing the problem.

Ideally (if you have the time and patience) you'll want to add these one at a time and resubmit to the online judge after each one. When the result moves from Wrong Answer back to Runtime Error, you'll know you've found the exception that is causing the problem. From here, hopefully this will help you narrow down what to focus on (to fix) in your source code solution.

Good luck.



来源:https://stackoverflow.com/questions/6428569/runtime-error-in-uva-online-judge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!