Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class PokemonDontGo {
- public static void main(String[] args) {
- // instantiate scanner class read input and convert to list of integer
- Scanner sc = new Scanner(System.in);
- List<Integer> pokes = Arrays.stream(sc.nextLine().split("\\s+")).map(Integer::parseInt).collect(Collectors.toList());
- // declare int variable for summary of catches
- int sumOfPokes = 0;
- // loop until no pokes left
- while (!pokes.isEmpty()){
- // get int from scanner (console)
- int index = Integer.parseInt(sc.nextLine());
- // declare int variable actual pokemon
- int actualPoke = 0;
- // on negative index remove first index and copy last to first index
- if (index < 0){
- actualPoke = pokes.get(0);
- pokes.remove(0);
- pokes.add(0, pokes.get(pokes.size() - 1));
- }
- // on index greater than list size remove last index and copy first to last index
- else if (index > pokes.size() - 1){
- actualPoke = pokes.get(pokes.size() - 1);
- pokes.remove(pokes.size() - 1);
- pokes.add(pokes.get(0));
- }
- // on index in range of list remove index
- else {
- actualPoke = pokes.get(index);
- pokes.remove(index);
- }
- // when the list is not empty calculate all remaining values
- if (!pokes.isEmpty()){
- for (int i = 0; i < pokes.size(); i++){
- int p = pokes.get(i);
- if (p <= actualPoke){
- p += actualPoke;
- }
- else {
- p -= actualPoke;
- }
- pokes.remove(i);
- pokes.add(i, p);
- }
- }
- // sum the actual removed Pokemon
- sumOfPokes += actualPoke;
- }
- // print the result to console
- System.out.println(sumOfPokes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment