SPOJ上java中的错误

时间:2016-10-20 04:19:14

标签: java math error-handling

问题

http://www.spoj.com/problems/EIGHTS/

必须从头开始打印第k个数字,其中多维数据集的最后3位为888。它遵循一种模式:192,442,692,942,1192,1442,1692,1942,2192,2442,2692,2942等。

我的解决方案

import java.util.Scanner;

 class Main {

    public static void main(String[] args)throws java.lang.Exception {
        Scanner p = new Scanner(System.in);
        for(int i=p.nextInt();i>0;i--){
            int k = p.nextInt();
            int A[] = {192,442,692,942};
            int l = 0;
            while(l<k){
                if((l!=0)&&(l%4 == 0)){
                    A[0]+=1000;
                    A[1]+=1000;
                    A[2]+=1000;
                    A[3]+=1000;
                }
                l++;
            }
            System.out.println(A[(l-1)%4]);
        }
    }
}

SPOJ说 NZEC错误

帮我弄清楚我错在哪里。

1 个答案:

答案 0 :(得分:0)

因为您将在int中存储k = 2000000000000这是不可能的,因此它会溢出并且可能存储负数,您正在访问它以获取数组索引。因此它实际上是arrayIndexOutOFBounds问题。要检查你可以使用try-catch for ArrayIndexOutOFBounds并打印一些东西。这次会给出错误的答案。