8 / 8
Nov 2018

hi
i have tried to solve this spoj.pl/problems/ANARC08E/14
it's working fine when i try it but when i upload the answer i get NZEC so plz someone help!
this is my code

package javaapplication1;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int arr[][] = new int[100][2];
   
    int x = sc.nextInt();
    int y = sc.nextInt();
    
    int ii = 0;
    while ((x!=-1)&&(y!=-1)){
        arr[ii][0] = x;
        arr[ii][1] = y;
        ii++;
         x = sc.nextInt();
     y = sc.nextInt();
    }

    for (int i = 0; i < ii; i++) {
        

            x = arr[i][0];

            y = arr[i][1];
            if ((x == 1) || (y == 1)){

                System.out.println(x + "+" + y + "=" + (x + y));
            }
 else{
System.out.println(x + "+" + y + " !=" + (x + y));
 }
        }
 
   
  
}
}
  • created

    Mar '11
  • last reply

    Nov '18
  • 7

    replies

  • 3.6k

    views

  • 6

    users

  • 3

    links

What makes you think there are at most 100 inputs?

Input each test case, output the result of that test case, read the next case.

well i did that because u can't do this in java
int x[] = new int[][2]//wrong
anyway it wouldn't cause NZEC (would it)??

Sure it would. If there were 101 inputs then it would exceed array bounds, throw and exception, and therefore return a non-zero exit code (NZEC).

Process test cases one at a time, input a test case, output a result, get the next test case.

3 years later

thats cos i found something wierd when i use a single scanner for reading both int and string.I would recommend you to try that it will accept 1 less input for each test case.when i used 2 scanners it ran fine(in my pc)

2 years later

hi
i have tried to solve this http://www.spoj.com/problems/ARITH2/12
it's working fine when i try it but when i upload the answer i get NZEC so plz someone help!
this is my code

import java.util.*;
import java.lang.*;

class Main
{
static int oper(int op1, String c, int op2) {
switch (c) {
case "+":
return (op1 + op2);
case "-":
return (op1 - op2);
case "*":
return (op1 * op2);
case "/":
try{
return (op1 / op2);
}catch(ArithmeticException e)
{
System.out.println("ERROR");
}
}
return 0;
}
public static void main (String[] args) throws java.lang.Exception
{
try{
Scanner input = new Scanner(System.in);
int value = Integer.parseInt(input.nextLine());
String[] answer = new String[value];
String[] num;
int w = 0;
for (w = 0; w < value; w++) {
num = (input.nextLine()).split(" ");
for (int n = 0; n < num.length - 2; n += 2) {
num[n + 2] = "" + oper(Integer.parseInt(num[n]), num[n + 1], Integer.parseInt(num[n + 2]));
}
answer[w] = num[((num.length - 1) / 2) * 2];
}

    }catch(ArrayIndexOutOfBoundsException e)
    {
        System.out.println("ERROR");;
    }
}

}

3 months later
1 year later

I was trying to solve Wormholes (Problem Code: ZCO12002) on CodeChef. I am getting AC on test-cases 0,9 and 15. All others are giving RE (NZEC). I am iterating over the contests and then searching for the best wormhole through modified Binary Search.
Here is my code -

import java.util.*;
import java.io.*;

class Main{
public static void main(String[] args) throws IOException{
	Main m = new Main();
	m.start();
}

public void start() throws IOException{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String[] line1 = br.readLine().split(" ");
	int n = Integer.parseInt(line1[0]);
	int x = Integer.parseInt(line1[1]);
	int y = Integer.parseInt(line1[2]);
	Contest[] contests = new Contest[n];
	for(int i = 0;i<n;i++){
		String[] line = br.readLine().split(" ");
		Contest c = new Contest(Integer.parseInt(line[0]),Integer.parseInt(line[1]));
		contests[i] = c;
	}
	int[] entries = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
	int[] exits = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
	Arrays.sort(entries);
	Arrays.sort(exits);
	int least_time_spent = Integer.MAX_VALUE;
	for(int i = 0;i<n;i++) {
		Contest c= contests[i];
		int start = searchA(entries,0,x-1,c.start);
		int end = searchB(exits,0,y-1,c.end);
		least_time_spent = Math.min(least_time_spent,end-start+1);
	}
	System.out.println(least_time_spent);
}

class Contest{
	int start;
	int end;
	public Contest(int s, int e){
		start = s;
		end = e;
	}
}

public int searchA(int[] a,int left,int right, int o){//BinarySearch for max value less than or equal o
	int mid = (left+right)/2;
	if(left-right==0){
		if(a[left-1]>o){
			return -1;
		}
		return a[left-1];
	}else if(a[mid]==o){
		return a[mid];
	}else if(o>a[mid]){
		return searchA(a,mid+1,right,o);
	}else if(o<a[mid]){
		return searchA(a,left,mid,o);
	}else{
		return 0;
	}
}

public int searchB(int[] a,int left,int right, int o){//BinarySearch for min value greater than or equal o
	int mid = (left+right)/2;
	if(right-left==0){
		if(a[left]<o){
			return Integer.MAX_VALUE;
		}
		return a[left];
	}else if(a[mid]==o){
		return a[mid];
	}else if(o>a[mid]){
		return searchB(a,mid+1,right,o);
	}else if(o<a[mid]){
		return searchB(a,left,mid,o);
	}else{
		return 0;
	}
}
}

Feel free to ask for any clarifications. It would be a great help if you can tell me the test-case where my code is throwing exception.