Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] boxes = scanner.nextLine().split(" ");
- String[] magic = scanner.nextLine().split(" ");
- ArrayDeque<Integer> boxesValues = new ArrayDeque<>();
- ArrayDeque<Integer> magicValues = new ArrayDeque<>();
- for (String box : boxes) {
- boxesValues.push(Integer.parseInt(box));
- }
- for (String value : magic) {
- magicValues.offer(Integer.parseInt(value));
- }
- int dollCounter = 0;
- int woodenTrainCounter = 0;
- int teddyBearCounter = 0;
- int bicycleCounter = 0;
- while (!boxesValues.isEmpty() && !magicValues.isEmpty()) {
- //napravi si promenlivi, za d (b,m ) a izbegnesh nullPointerExeption
- int b = boxesValues.peek();
- int m = magicValues.peek();
- int sum = boxesValues.peek() * magicValues.peek();
- //tuk e dobre da pochnesh da proverqvash s negativnite rezultati parvo
- if (sum < 0) {
- sum = boxesValues.peek() + magicValues.peek();
- remove(boxesValues, magicValues);
- boxesValues.push(sum);
- }
- else if (m == 0 || b == 0){
- //tuk trqbva da e dva pati if, an ne if / else if, zashtoto inache ako i dvete sa nula, samo ednoto shte se mahne
- if(m == 0){
- // ne s remove()
- magicValues.poll();
- }
- if(b == 0){
- boxesValues.pop();
- }
- }
- else if (sum == 150) {
- dollCounter++;
- remove(boxesValues, magicValues);
- } else if (sum == 250) {
- woodenTrainCounter++;
- remove(boxesValues, magicValues);
- } else if (sum == 300) {
- teddyBearCounter++;
- remove(boxesValues, magicValues);
- } else if (sum == 400) {
- bicycleCounter++;
- remove(boxesValues, magicValues);
- }
- else {
- magicValues.poll();
- int newMaterial= b + 15;
- boxesValues.pop();
- boxesValues.push(newMaterial);
- }
- }
- // parvo printirash saobshtenieto
- if(dollCounter > 0 && woodenTrainCounter > 0 || bicycleCounter > 0 && teddyBearCounter > 0){
- System.out.println("The presents are crafted! Merry Christmas!");
- }
- else {
- System.out.println("No presents this Christmas!");
- }
- //proverqvash purvo dali sa prazni ili ne i printirash ako ne sa
- if (boxesValues.size() > 0 ) {
- System.out.print("Materials left: ");
- for (int i = 0; i < boxesValues.size(); i++) {
- if (i == boxesValues.size() - 1) {
- System.out.print(boxesValues.pop());
- } else {
- System.out.print(String.format("%d, ", boxesValues.pop()));
- i--;
- }
- }
- }
- if (magicValues.size() > 0 ) {
- System.out.print("Materials left: ");
- for (int i = 0; i < boxesValues.size(); i++) {
- if (i == boxesValues.size() - 1) {
- System.out.print(boxesValues.poll());
- } else {
- System.out.print(String.format("%d, ", magicValues.poll()));
- i--;
- }
- }
- }
- System.out.println();
- //tova printirane go pravish s TreeMap, zashtoto igrachkite trqbvA da se sortirat po azbuchen red
- print(dollCounter, woodenTrainCounter, teddyBearCounter, bicycleCounter);
- }
- private static void print(int dollCounter, int woodenTrainCounter, int teddyBearCounter, int bicycleCounter) {
- if(dollCounter > 0){
- System.out.println(String.format("Doll: %d", dollCounter));
- }
- if(woodenTrainCounter > 0){
- System.out.println(String.format("Wooden train: %d", woodenTrainCounter));
- }
- if(teddyBearCounter > 0){
- System.out.println(String.format("Teddy bear: %d", teddyBearCounter));
- }
- if(bicycleCounter > 0){
- System.out.println(String.format("Bicycle: %d", bicycleCounter));
- }
- }
- private static void remove(ArrayDeque<Integer> boxes, ArrayDeque<Integer> magics) {
- boxes.pop();
- magics.poll();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement