Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.31 KB | None | 0 0
  1. package Lesson05ObjectsClassesAndAPIs.Exercise;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayDeque;
  5. import java.util.Deque;
  6.  
  7. public class Pr04TruckTourAlgorithmsTest {
  8.  
  9.     public static void main(String[] args) throws IOException {
  10.         int pumpsCount = 20000 + 2;
  11.         int[] fuelPumps = new int[pumpsCount];
  12.         Deque<Integer> fuelPumpsQueue = new ArrayDeque<>(pumpsCount);
  13.  
  14.         fuelPumps[pumpsCount - 2] = -1;
  15.         fuelPumps[pumpsCount - 1] = 1;
  16.  
  17.         for (int i = 0; i < pumpsCount - 2; i++) {
  18.             fuelPumpsQueue.addLast(0);
  19.         }
  20.         fuelPumpsQueue.addLast(-1);
  21.         fuelPumpsQueue.addLast(1);
  22.  
  23.         System.out.println("Number of elements (fuel pumps): " + pumpsCount);
  24.         System.out.println();
  25.  
  26.         getStartIndexLinearAlgorithm(pumpsCount, fuelPumps);
  27.         System.out.println();
  28.  
  29.         getStartIndexQuadraticAlgorithm(pumpsCount, fuelPumpsQueue);
  30.         System.out.println();
  31.     }
  32.  
  33.     private static void getStartIndexQuadraticAlgorithm(int pumpsCount, Deque<Integer> fuelPumpsQueue) {
  34.         System.out.println("Quadratic Algorithm (queue)...");
  35.         long counter = 0L;
  36.         int startIndex = 0;
  37.         Long ourFuel;
  38.         boolean found;
  39.  
  40.         long lStartTime = System.nanoTime();
  41.         while (true) {
  42.             ourFuel = 0L;
  43.             found = true;
  44.             for (int fuel : fuelPumpsQueue) {
  45.                 counter++;
  46.                 ourFuel += fuel;
  47.                 if (ourFuel < 0L) {
  48.                     found = false;
  49.                     break;
  50.                 }
  51.             }
  52.  
  53.             if (found) {
  54.                 break;
  55.             }
  56.             fuelPumpsQueue.addLast(fuelPumpsQueue.pop());
  57.             startIndex++;
  58.         }
  59.         long lEndTime = System.nanoTime();
  60.         long output = lEndTime - lStartTime;
  61.         System.out.println("Elapsed time in milliseconds: " + output / 1000000);
  62.  
  63.         System.out.println("Counter: " + counter);
  64.         System.out.println("Start index: " + startIndex);
  65.     }
  66.  
  67.     private static void getStartIndexLinearAlgorithm(int pumpsCount, int[] fuelPumps) {
  68.         System.out.println("Linear Algorithm (array)...");
  69.         long counter = 0L;
  70.         int startIndex = 0;
  71.         int currentIndex = 0;
  72.         int visitedPumps = 0;
  73.         long currentFuel = 0L;
  74.  
  75.         long lStartTime = System.nanoTime();
  76.         while (pumpsCount > visitedPumps) {
  77.             counter++;
  78.             if (fuelPumps[currentIndex] + currentFuel < 0L) {
  79.                 if (visitedPumps > 0) {
  80.                     visitedPumps--;
  81.                     currentFuel -= fuelPumps[startIndex];
  82.                 } else {
  83.                     currentIndex++;
  84.                 }
  85.                 startIndex++;
  86.             } else {
  87.                 currentFuel += fuelPumps[currentIndex];
  88.                 visitedPumps++;
  89.                 currentIndex++;
  90.                 if (currentIndex >= pumpsCount) {
  91.                     currentIndex = 0;
  92.                 }
  93.             }
  94.         }
  95.         long lEndTime = System.nanoTime();
  96.         long output = lEndTime - lStartTime;
  97.         System.out.println("Elapsed time in milliseconds: " + output / 1000000);
  98.  
  99.         System.out.println("Counter: " + counter);
  100.         System.out.println("Start index: " + startIndex);
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement