RGAlex

1O. Lady Bugs

Feb 2nd, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. package ArraysExercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class LadyBugs10 {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int fieldSize = Integer.parseInt(scanner.nextLine());
  10.         int[] field = new int[fieldSize];
  11.  
  12.         int[] ladyBugsIndexes = Arrays.stream(scanner.nextLine().split(" "))
  13.                 .mapToInt(Integer::parseInt).toArray();
  14.  
  15.         for (int ladyBugIndex : ladyBugsIndexes) {
  16.             if (ladyBugIndex >= 0 && ladyBugIndex <= field.length - 1) {
  17.                 field[ladyBugIndex] = 1;
  18.             }
  19.         }
  20.         String command = scanner.nextLine();
  21.  
  22.         while (!command.equals("end")) {
  23.  
  24.             String[] tokens = command.split(" ");
  25.             int index = Integer.parseInt(tokens[0]);
  26.             String direction = tokens[1];
  27.             int flyLength = Integer.parseInt(tokens[2]);
  28.  
  29.  
  30.             if (index >= 0 && index <= field.length - 1 && field[index] == 1) {
  31.  
  32.                 field[index] = 0;
  33.                 if (direction.equals("right")) {
  34.                     index += flyLength;
  35.  
  36.                     while (index <= field.length - 1 && field[index] == 1) {
  37.                         index += flyLength;
  38.                     }
  39.  
  40.                     if (index <= field.length - 1) {
  41.                         field[index] = 1;
  42.  
  43.                     }
  44.                 } else if (direction.equals("left")) {
  45.                     index -= flyLength;
  46.  
  47.                     while (index >= 0 && field[index] == 1) {
  48.                         index -= flyLength;
  49.                     }
  50.                     if (index >= 0) {
  51.                         field[index] = 1;
  52.                     }
  53.                 }
  54.             }
  55.             command = scanner.nextLine();
  56.         }
  57.         for (int number : field) {
  58.             System.out.print(number + " ");
  59.         }
  60.     }
  61. }
Add Comment
Please, Sign In to add comment