Guest User

Untitled

a guest
Dec 13th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. package Lab3;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8. import java.util.ArrayList;
  9.  
  10. public class Lab3
  11. {
  12.     public static void main(String[] args) throws FileNotFoundException, IOException
  13.     {
  14.         Scanner scan;
  15.         BufferedReader in = new BufferedReader(new FileReader(args[0]));
  16.         String line;
  17.         ArrayList<Integer> arrayList = new ArrayList<Integer>();
  18.         int lineSize = 0;
  19.         int iteratePrint;
  20.         int indexOfLargest;
  21.         int j;
  22.         int n;
  23.         int count = 0;
  24.         /*
  25.          * Counting the lines total to assign to count.
  26.          */
  27.         while((line = in.readLine()) != null){
  28.             arrayList.clear();
  29.             scan = new Scanner(line);
  30.             while(scan.hasNextInt()){
  31.                 arrayList.add(scan.nextInt());
  32.             }
  33.             n = arrayList.size();
  34.             for(j = n-1; j >= 0; j--){
  35.                 if(arrayList.get(j) == n){
  36.                     indexOfLargest = j;
  37.                     if(indexOfLargest == n-1){ //element is in correct spot
  38.                         continue;
  39.                     }  
  40.                     if(indexOfLargest == 0){ //if(largest is at arraylist[0]){ flip wholestack
  41.                         flip(n/2, arrayList);
  42.                     }
  43.                     else{
  44.                         flip(indexOfLargest, arrayList);
  45.                         flip(n/2, arrayList);
  46.                     }
  47.                 }
  48.             }
  49.             for(iteratePrint = 0; iteratePrint < arrayList.size(); iteratePrint++){
  50.                 System.out.println(arrayList.get(iteratePrint));
  51.                 System.out.println("");
  52.             }
  53.         }
  54.     }
  55.  
  56.     public static void flip(int index, ArrayList<Integer> pancakes)
  57.     {
  58.         int i = 0;
  59.         int j = index;
  60.         int temp;
  61.         while(i <= index / 2){
  62.             temp = pancakes.get(j);
  63.             pancakes.set(j, pancakes.get(i));
  64.             pancakes.set(i, temp);
  65.             i++;
  66.             j--;
  67.         }
  68.     }
  69. }
Add Comment
Please, Sign In to add comment