Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class Pokemons {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11. List<Integer> pokemonDistance = Arrays.stream(scanner.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
  12.  
  13. int valueOfRemovedElements = 0;
  14. while (!pokemonDistance.isEmpty()) {
  15. int command = Integer.parseInt(scanner.nextLine());
  16. int indexToRemove = 0;
  17. if (command < 0) {
  18. indexToRemove = pokemonDistance.remove(0);
  19. valueOfRemovedElements += indexToRemove;
  20. pokemonDistance.add(0, pokemonDistance.get(pokemonDistance.size() - 1));
  21. } else if (command > pokemonDistance.size() - 1) {
  22. indexToRemove = pokemonDistance.remove(pokemonDistance.size() - 1);
  23. pokemonDistance.add(pokemonDistance.get(0));
  24. valueOfRemovedElements += indexToRemove;
  25. } else {
  26. indexToRemove = pokemonDistance.remove(command);
  27. valueOfRemovedElements += indexToRemove;
  28. }
  29.  
  30. for (int i = 0; i < pokemonDistance.size(); i++) {
  31. if (pokemonDistance.get(i) <= indexToRemove) {
  32. pokemonDistance.set(i, pokemonDistance.get(i) + indexToRemove);
  33. } else {
  34. pokemonDistance.set(i, pokemonDistance.get(i) - indexToRemove);
  35. }
  36. }
  37. }
  38.  
  39. System.out.println(valueOfRemovedElements);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement