Advertisement
svephoto

Ladybugs [Java]

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