运行时错误(NZEC) - java / Codechef

时间:2018-04-13 08:29:55

标签: java

我一直在尝试codechef中的CLEANUP问题,但是,当我向法官提交时,它会显示IDE上的NZEC运行时错误和运行时错误。 请帮我理解我哪里出错了。

   public static void main(String[] args) {
        //initialize the scanner.
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); //no. of test cases.
        for (int r = 0; r <= t; r++) {

            int n = sc.nextInt(); //no. of jobs to be done.
            int m = sc.nextInt(); //no.of jobs already completed.

            ArrayList<Integer> al = new ArrayList<>();
            //add the no. of jobs to be done
            for (int i = 0; i < n; i++) {
                int x = i + 1;
                al.add(x);
            }
            //remove no. of jobs already done
            for (int i = 0; i < m; i++) {
                int q = sc.nextInt();
                Integer a = new Integer(q);
                al.remove(a);
            }

            //print jobs to be done by chef
            for (int i = 0; i < al.size(); i += 2) {
                System.out.println(al.get(i) + " ");
            }
            System.out.println();
            //print jobs to be done by asst.
            for (int i = 1; i < al.size(); i += 2) {
                System.out.println(al.get(i) + " ");
            }
            System.out.println();
            al.clear();

        }

    }

1 个答案:

答案 0 :(得分:0)

问题是测试用例的数量。 for(int r=0;r<=t;r++)r应从1开始。

这是NZEC runtime error的原因。 此外,您的输出不符合相关提及的格式。

看看这段代码:

    //print jobs to be done by chef
    for (int i = 0; i < al.size(); i += 2) {
        System.out.println(al.get(i) + " ");
    }

println()正在新行中打印厨师任务。

同样,

    //print jobs to be done by asst.
    for (int i = 1; i < al.size(); i += 2) {
        System.out.println(al.get(i) + " ");
    }

因此,只使用print()。

所以,这是最终的代码:

import java.util.ArrayList;
import java.util.Scanner;

class Solution {

    public static void main(String[] args) {
        //initialize the scanner.
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt(); //no. of test cases.
        for (int r = 0; r < t; r++) {

            int n = sc.nextInt(); //no. of jobs to be done.
            int m = sc.nextInt(); //no.of jobs already completed.

            ArrayList<Integer> al = new ArrayList<>();
            //add the no. of jobs to be done
            for (int i = 0; i < n; i++) {
                int x = i + 1;
                al.add(x);
            }
            //remove no. of jobs already done
            for (int i = 0; i < m; i++) {
                int q = sc.nextInt();
                Integer a = new Integer(q);
                al.remove(a);
            }

            //print jobs to be done by chef
            for (int i = 0; i < al.size(); i += 2) {
                System.out.print(al.get(i) + " ");
            }
            System.out.println();
            //print jobs to be done by asst.
            for (int i = 1; i < al.size(); i += 2) {
                System.out.print(al.get(i) + " ");
            }
            System.out.println();
            al.clear();

        }

    }

}