Advertisement
Guest User

Untitled

a guest
Jun 21st, 2019
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class MakeASalad {
  7. public static void main(String[] args){
  8. Scanner scanner=new Scanner(System.in);
  9. Map<String,Integer> veggies=new LinkedHashMap<String,Integer>(){{
  10. put("tomato",80);
  11. put("carrot",136);
  12. put("lettuce",109);
  13. put("potato",215);
  14. }};
  15. ArrayDeque<String> vegetables=new ArrayDeque<>();
  16. Arrays.stream(scanner.nextLine().split("\\s+")).filter(x -> x.matches("(tomato|carrot|lettuce|potato)")).forEach(vegetables::offer);
  17. ArrayDeque<Integer> salads=new ArrayDeque<>();
  18. Arrays.stream(scanner.nextLine().split("\\s+")).mapToInt(Integer::parseInt).forEach(salads::push);
  19. while (!vegetables.isEmpty() && !salads.isEmpty()) {
  20. int currentSalad = salads.peek();
  21.  
  22. while (currentSalad > 0 && !vegetables.isEmpty()) {
  23. currentSalad -= veggies.get(vegetables.poll());
  24. }
  25. System.out.print(salads.pop() + " ");
  26. }
  27. System.out.println();
  28.  
  29. if(!salads.isEmpty()){
  30. System.out.println(salads.toString().replaceAll("[\\[\\],]",""));
  31. }
  32.  
  33. if(!vegetables.isEmpty()){
  34. System.out.println(vegetables.toString().replaceAll("[\\[\\],]",""));
  35. }
  36.  
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement