/* * Following is a sample Java code which is meant for handling the most common * input format used by ACM (as well as Programming Club) * The input format is first line contains t, the number of test cases * followed by the cases (each of which has its own particular format) */ import java.io.*; class prog { public static void main(String args[]) { int t; String tstr=""; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); try { tstr = br.readLine(); } catch(IOException e) { System.out.println(e); } t=Integer.parseInt(tstr);//Safely assume that input is number, We dont check robustmess, emphasis is on algorithms while(t-->0) { //your code goes here } } /* * If this file is saved as prob1.java * then to compile use the command "javac prob1.java" * and to run the file use the command "java prob1.java" */ } /*The following code block is useful in java to have cin and scanf kind of functionality */ InputStreamReader isr = new InputStreamReader ( System.in ); StreamTokenizer st = new StreamTokenizer ( isr ); /* Once you have a StreamTokenizer, you can customize it's behavior. For this example we just show basic usage. */ int c; try { while ( true ) { switch ( c = st.nextToken () ) { case StreamTokenizer.TT_NUMBER: System.out.println ( "Read a number: " + st.nval ); break; case StreamTokenizer.TT_WORD: System.out.println ( "Read a word: " + st.sval ); break; case default: // do something in other cases (EOL, EOF, whitespace, quotes...) break; } } catch ( IOException ioe ) { // Won't happen very often from the keyboard }