Didart

Shoot for the Win

Oct 23rd, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.87 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.stream.Collectors;
  5.  
  6. public class ShootForTheWin {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         List<Integer> targetsSequence = Arrays
  11.                 .stream(scanner.nextLine().split(" "))
  12.                 .map(Integer::parseInt)
  13.                 .collect(Collectors.toList());
  14.  
  15.         int shots = 0;
  16.         String input = scanner.nextLine();
  17.         while (!input.equals("End")) {
  18.             int indexTarget = Integer.parseInt(input);
  19.  
  20.             if (isValidIndex(indexTarget, targetsSequence.size() - 1) && targetsSequence.get(indexTarget) != -1) {
  21.  
  22.                 int currentNumber = targetsSequence.get(indexTarget);
  23.                 targetsSequence.set(indexTarget, -1);
  24.                 for (int i = 0; i <= targetsSequence.size() - 1; i++) {
  25.  
  26.                     if (targetsSequence.get(i) != -1) {
  27.                         if (targetsSequence.get(i) > currentNumber) {
  28.                             targetsSequence.set(i, targetsSequence.get(i) - currentNumber);
  29.                         } else {
  30.                             targetsSequence.set(i, targetsSequence.get(i) + currentNumber);
  31.                         }
  32.                     }
  33.                 }
  34.                 shots++;
  35.             } else if (!isValidIndex(indexTarget, targetsSequence.size() - 1)) {
  36.  
  37.             }
  38.  
  39.             input = scanner.nextLine();
  40.         }
  41.         String output = "";
  42.         for (int i = 0; i < targetsSequence.size(); i++) {
  43.             output += targetsSequence.get(i) + " ";
  44.         }
  45.         System.out.println("Shot targets: " + shots + " -> " + output);
  46.         scanner.close();
  47.     }
  48.  
  49.     private static boolean isValidIndex(int indexTarget, int i) {
  50.         return indexTarget >= 0 && indexTarget <= i;
  51.     }
  52. }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment