运行时错误:NZEC

时间:2012-07-24 16:11:34

标签: java runtime-error

我在http://www.codechef.com/problems/LASTDIG处对问题的提交时遇到运行时错误。

我的代码如下:

public class Main {
    public static void main(String[] args) {
        try {
            Scanner in = new Scanner(System.in);
            int i,j;
            System.out.println("Enter the no. of test cases - ");
            int cases = in.nextInt();

            int[][] a = new int[cases][2];
            int[] lds = new int[cases]; 

            for(i=0; i<cases; i++) {
                for(j=0; j<2; j++) {
                    a[i][j]=in.nextInt();
                }
            }

            for(j=0; j<cases; j++) {
                lds[j] = 0;
                int LENGTH = a[j][1] - a[j][0] +1;
                int[] arr = new int[LENGTH];
                //System.out.printf("%d\n",LENGTH);
                int[]sum = new int[LENGTH];

                for(i=0; i<LENGTH; i++) {
                    sum[i] = 0;
                    arr[i] = a[j][0] + i;
                }

                for(i=0; i<LENGTH; i++) {
                    int temp = arr[i];
                    while(temp !=0 ) {
                        int r = temp%10;
                        temp /= 10;

                        if (r%2 == 0) sum[i] += r*2;
                        else sum[i] += r;
                    }
                }

                for(i=0; i<LENGTH; i++){
                    lds[j] += sum[i]%10;
                }

            }

            for(i=0; i<cases; i++) {
                System.out.printf("%d\n",lds[i]);
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}

请在这里帮助我,考虑到问题很简单。

谢谢!

1 个答案:

答案 0 :(得分:0)

代码本身运行良好......给出合理的输入。对于不同的输入,可能会发生错误,如:

  1. 如果案例不是整数怎么办?
  2. 如果案例是负整数怎么办?
  3. 因此,正如编辑试图完成的那样,我们需要为不可预见的用例提供合理的反馈。

    但对于链接中的问题,还有其他潜在问题。溢出?对于非常大的数字,溢出不会存在,2 ^ 32大于最大数字,但我注意到一个大整数可以给出OutOfMemory错误,所以我认为这一点代码归结为低效率和需要更好算法处理更大的整数。

相关问题