0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

class Main {

    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));        
        int t = Integer.parseInt(input.readLine());

        for (int i = 0; i < t; i++) {
            String[] arrayData = input.readLine().split(" ");
            int[] cardsi = new int[arrayData.length];
            int e = 0;

            for(int a = 0; a < arrayData.length; a++){
                cardsi[e] = Integer.parseInt(arrayData[a]);
                e++;
            }

            int X = cardsi[0];
            int N = cardsi[1];
            long count = 0;

            for (int j = 2; j < cardsi.length; j++) {
                for (int l = 3; l <= (cardsi.length - 1); l++) {
                    if ((cardsi[j] + cardsi[l]) == X &&(j != l)) {
                        count++;
                    }
                }
            }
            System.out.println((i + 1) + ". " + count);
        }

    }
}

在 spoj 上提交此 LCPC12F 问题时出现超出时间限制的错误。什么可能是解决方案?扫描仪是出现此类错误的主要麻烦吗?

4

1 回答 1

0

Have you ever run those codes on IDE?

When I give the number to t element then arrayData (in this case, we should enter int type not String, because of the java.lang.NumberFormatException), it shows Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 and int N = cardsi[1]; is the main problem on your code I think. This is because String[] arrayData = input.readLine().split(" "); size is 1 so you do not have cardsi[1] element on your int array

于 2018-07-02T08:43:35.837 回答