Advertisement
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 MovingTarget_03 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] parts = scanner.nextLine().split("\\s+");
- List<Integer> numbers = Arrays.stream(parts)
- .map(Integer::parseInt)
- .collect(Collectors.toList());
- String input = scanner.nextLine();
- while (!"End".equals(input)) {
- String[] tokens = input.split("\\s+");
- String command = tokens[0];
- int index = Integer.parseInt(tokens[1]);
- int element = Integer.parseInt(tokens[2]);
- switch (command) {
- case "Shoot":
- if (isValid(numbers, index)) {
- if (numbers.get(index) > element) {
- int sum = numbers.get(index) - element;
- numbers.remove(index);
- numbers.add(sum);
- } else {
- numbers.remove(index);
- }
- }
- break;
- case "Add":
- if (isValid(numbers, index)) {
- numbers.add(index, element);
- } else {
- System.out.println("Invalid placement!");
- }
- break;
- case "Strike":
- int leftRadius = Math.max(index - element, 0);
- int rightRadius = Math.min(index + leftRadius, numbers.size() - 1);
- if (leftRadius >= 0 && rightRadius < numbers.size() - element) {
- for (int i = rightRadius; i >= leftRadius; i--) {
- numbers.remove(i);
- }
- } else {
- System.out.println("Strike missed!");
- }
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println(numbers.toString().replaceAll("[\\[\\],]", "")
- .replaceAll(" ", "|"));
- }
- private static boolean isValid(List<Integer> numbers, int index) {
- boolean result = index >= 0 && index < numbers.size();
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement