SHARE
TWEET

Untitled

a guest Sep 21st, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package comp2402a1;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. import java.util.*;
  10.  
  11. public class Part7 {
  12.    
  13.     /**
  14.      * Your code goes here - see Part0 for an example
  15.      * @param r the reader to read from
  16.      * @param w the writer to write to
  17.      * @throws IOException
  18.      */
  19.     public static void doIt(BufferedReader r, PrintWriter w) throws IOException {
  20.            
  21.         Deque<String> deque = new LinkedList<String>();
  22.         int greater = 0;
  23.         int less = 0;
  24.         String first = r.readLine();
  25.         deque.add(first);
  26.         for (String line = r.readLine(); line != null; line = r.readLine()) {
  27.             if (deque!= null && deque.getFirst() != null ) {
  28.                 if (line.compareTo(deque.getFirst()) < 0 )
  29.                 {
  30.                     greater++;
  31.                     deque.addFirst(line);
  32.                 }
  33.             }
  34.             if (deque!= null && deque.getLast() != null ) {
  35.                 if (line.compareTo(deque.getLast()) > 0 )
  36.                 {
  37.                     less++;
  38.                     deque.addLast(line);
  39.                 }
  40.             }
  41.             if (greater > 1000 && less > 1000) {
  42.                 w.println(line);
  43.             }
  44.            
  45.         }
  46.            
  47.     }
  48.  
  49.     /**
  50.      * The driver.  Open a BufferedReader and a PrintWriter, either from System.in
  51.      * and System.out or from filenames specified on the command line, then call doIt.
  52.      * @param args
  53.      */
  54.     public static void main(String[] args) {
  55.         try {
  56.             BufferedReader r;
  57.             PrintWriter w;
  58.             if (args.length == 0) {
  59.                 r = new BufferedReader(new InputStreamReader(System.in));
  60.                 w = new PrintWriter(System.out);
  61.             } else if (args.length == 1) {
  62.                 r = new BufferedReader(new FileReader(args[0]));
  63.                 w = new PrintWriter(System.out);               
  64.             } else {
  65.                 r = new BufferedReader(new FileReader(args[0]));
  66.                 w = new PrintWriter(new FileWriter(args[1]));
  67.             }
  68.             long start = System.nanoTime();
  69.             doIt(r, w);
  70.             w.flush();
  71.             long stop = System.nanoTime();
  72.             System.out.println("Execution time: " + 10e-9 * (stop-start));
  73.         } catch (IOException e) {
  74.             System.err.println(e);
  75.             System.exit(-1);
  76.         }
  77.     }
  78. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top