Share Pastebin
Guest
Public paste!

repsilat

By: a guest | Nov 6th, 2009 | Syntax: Java | Size: 0.91 KB | Hits: 41 | Expires: Never
Copy text to clipboard
  1. //(c) Matthew Steel 2009-, no warranty etc, licensed under the WTFPL v2.
  2. //Finds when all four digit numbers have gone through stdin.
  3.  
  4. import java.io.IOException;
  5. public class Searcher {
  6.  
  7.         public static void main(String[] args) {
  8.                 int unseen = 10000;
  9.                 boolean[] seen = new boolean[10000];
  10.  
  11.                 int number=0;
  12.                 for(int i = 0; i < 3; ++i)
  13.                         number = 10*number + (readChar());
  14.  
  15.                 int digitsSeen;
  16.                 for(digitsSeen = 3; unseen > 0; ++digitsSeen) {
  17.                         number = (10*number + (readChar()))%10000;
  18.                         if(!seen[number]) {
  19.                                 seen[number] = true;
  20.                                 --unseen;
  21.                         }
  22.                 }
  23.                 System.out.println(number);
  24.                 System.out.println(digitsSeen);
  25.         }
  26.         private static int readChar() {
  27.                 try {
  28.                         int c = System.in.read();
  29.                         if (c == -1) throw new IOException("End of file");
  30.                         //System.out.println("a");
  31.                         return c - '0';
  32.                 } catch (IOException ioe) {
  33.                         ioe.printStackTrace();
  34.                         System.exit(1);
  35.                 }
  36.                 return 0;
  37.         }
  38. }