Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.00 KB | None | 0 0
  1. public class Main {
  2.  
  3.     private static class List {
  4.         private List next;
  5.         private List previous;
  6.         private int value;
  7.  
  8.         public List() {
  9.             next = null;
  10.             previous = null;
  11.         }
  12.  
  13.         public List(int[] array) {
  14.             value = array[0];
  15.             List node = this;
  16.             for (int i = 1; i < array.length; i += 1) {
  17.                 node.next = new List();
  18.                 node.next.previous = node;
  19.                 node = node.next;
  20.                 node.value = array[i];
  21.             }
  22.         }
  23.  
  24.         public List get(int index) throws IndexOutOfBoundsException {
  25.             if (index < 0) {
  26.                 throw new IndexOutOfBoundsException();
  27.             }
  28.             List node = this;
  29.             for (int i = 0; i < index; i += 1) {
  30.                 if (node.next != null) {
  31.                     node = node.next;
  32.                 } else {
  33.                     throw new IndexOutOfBoundsException();
  34.                 }
  35.             }
  36.             return node;
  37.         }
  38.  
  39.         public void deletePrevious() {
  40.             previous = previous.previous;
  41.         }
  42.  
  43.         public void setNext(List node) {
  44.             next = node;
  45.         }
  46.  
  47.         public int getValue() {
  48.             return value;
  49.         }
  50.  
  51.         public List getNext() {
  52.             if (next == null) {
  53.                 throw new IndexOutOfBoundsException();
  54.             }
  55.             return next;
  56.         }
  57.  
  58.         public List getPrevious() {
  59.             if (previous == null) {
  60.                 throw new IndexOutOfBoundsException();
  61.             }
  62.             return previous;
  63.         }
  64.  
  65.         public void print() {
  66.             System.out.print(value + " ");
  67.             if (next != null) {
  68.                 next.print();
  69.             }
  70.         }
  71.     }
  72.  
  73.     private static int[] sell(int[] giftsArray, int[] customers) {
  74.         int[] sales = new int[customers.length];
  75.         List gifts = new List(giftsArray);
  76.         List gift = gifts.get(0);
  77.         boolean outOfStock = false;
  78.         for (int m = 0; m < customers.length; m += 1) {
  79.             if (outOfStock) {
  80.                 return sales;
  81.             }
  82.             while (true) {
  83.                 if (gift.getValue() <= customers[m]) {
  84.                     try {
  85.                         gift = gift.getNext();
  86.                     } catch (IndexOutOfBoundsException e0) {
  87.                         // if next does not exist then there is no
  88.                         // gift more expensive, thus, the customer
  89.                         // must buy this gift
  90.                         sales[m] = gift.getValue();
  91.                         try {
  92.                             gift = gift.getPrevious();
  93.                         } catch (IndexOutOfBoundsException e1) {
  94.                             // if there is no previous gift, then
  95.                             // the shop has run out of gifts to sell
  96.                             outOfStock = true;
  97.                             break;
  98.                         }
  99.                         gift.setNext(null);
  100.                         break;
  101.                     }
  102.                 } else {
  103.                     // try to sell previous gift
  104.                     try {
  105.                         sales[m] = gift.getPrevious().getValue();
  106.                         gift.deletePrevious();
  107.                     } catch (IndexOutOfBoundsException e) {
  108.                         // if previous does not exist, there is no gift
  109.                         // which this customer can afford
  110.                         sales[m] = 0;
  111.                     }
  112.                     break;
  113.                 }
  114.             }
  115.         }
  116.         return sales;
  117.     }
  118.  
  119.     public static void main(String[] args) {
  120.         int[] gifts = {1, 2, 2, 2, 5, 7, 10, 20};
  121.         int[] customers = {1,1,2,3,6,6,15,21,21,22};
  122.  
  123.         int[] sales = sell(gifts, customers);
  124.         for (int sale : sales) {
  125.             System.out.print(sale +  " ");
  126.         }
  127.         System.out.println();
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement