Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Lab3;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.ArrayList;
- public class Lab3
- {
- public static void main(String[] args) throws FileNotFoundException, IOException
- {
- Scanner scan;
- BufferedReader in = new BufferedReader(new FileReader(args[0]));
- String line;
- ArrayList<Integer> arrayList = new ArrayList<Integer>();
- int lineSize = 0;
- int iteratePrint;
- int indexOfLargest;
- int j;
- int n;
- int count = 0;
- /*
- * Counting the lines total to assign to count.
- */
- while((line = in.readLine()) != null){
- arrayList.clear();
- scan = new Scanner(line);
- while(scan.hasNextInt()){
- arrayList.add(scan.nextInt());
- }
- n = arrayList.size();
- for(j = n-1; j >= 0; j--){
- if(arrayList.get(j) == n){
- indexOfLargest = j;
- if(indexOfLargest == n-1){ //element is in correct spot
- continue;
- }
- if(indexOfLargest == 0){ //if(largest is at arraylist[0]){ flip wholestack
- flip(n/2, arrayList);
- }
- else{
- flip(indexOfLargest, arrayList);
- flip(n/2, arrayList);
- }
- }
- }
- for(iteratePrint = 0; iteratePrint < arrayList.size(); iteratePrint++){
- System.out.println(arrayList.get(iteratePrint));
- System.out.println("");
- }
- }
- }
- public static void flip(int index, ArrayList<Integer> pancakes)
- {
- int i = 0;
- int j = index;
- int temp;
- while(i <= index / 2){
- temp = pancakes.get(j);
- pancakes.set(j, pancakes.get(i));
- pancakes.set(i, temp);
- i++;
- j--;
- }
- }
- }
Add Comment
Please, Sign In to add comment