desislava_topuzakova

01. Bouquets

Feb 12th, 2022
939
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class Bouquets_01 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         String tulipsInput = scanner.nextLine();
  11.         String daffodilsInput = scanner.nextLine();
  12.  
  13.         //лалета -> стек
  14.         ArrayDeque<Integer> tulips = new ArrayDeque<>();
  15.         //нарциси -> опашка
  16.         ArrayDeque<Integer> daffodils = new ArrayDeque<>();
  17.  
  18.         //пълним стека с лалета
  19.         Arrays.stream(tulipsInput.split(",\\s+"))
  20.                 .mapToInt(Integer::parseInt)
  21.                 .forEach(tulips::push);
  22.  
  23.         //пълним опашка с нарциси
  24.         Arrays.stream(daffodilsInput.split(",\\s+"))
  25.                 .mapToInt(Integer::parseInt)
  26.                 .forEach(daffodils::offer);
  27.  
  28.         int bouquets = 0; //брой на букети
  29.         int leftFlowers = 0; //цветята, които не са станали на букет
  30.  
  31.         while (!tulips.isEmpty() && !daffodils.isEmpty()) {
  32.             int tulip = tulips.peek();
  33.             int daffodil = daffodils.peek();
  34.  
  35.             int sum = tulip + daffodil;
  36.             if (sum == 15) {
  37.                 //правя букет
  38.                 bouquets++;
  39.                 tulips.pop(); //премахва последния елемент добавен в стека
  40.                 daffodils.poll(); //премахва първия елемент добавен в опашката
  41.             } else if (sum > 15) {
  42.                 tulips.pop();
  43.                 tulips.push(tulip - 2);
  44.             } else if (sum < 15) {
  45.                 //съхраним сумата за по-късно
  46.                 leftFlowers += sum;
  47.                 tulips.pop(); //премахва последния елемент добавен в стека
  48.                 daffodils.poll(); //премахва първия елемент добавен в опашката
  49.             }
  50.         }
  51.         //брой на букетите
  52.         //брой на цветята, които не са образували букет
  53.         int bouquetsLeftFlowers = leftFlowers / 15;
  54.         bouquets += bouquetsLeftFlowers;
  55.  
  56.  
  57.         if (bouquets >= 5) {
  58.             System.out.printf("You made it! You go to the competition with %d bouquets!", bouquets);
  59.         } else {
  60.             System.out.printf("You failed... You need more %d bouquets.", 5 - bouquets);
  61.         }
  62.  
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment