Advertisement
Edzhevit

LadyBugs

Dec 19th, 2018
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. package ArraysExercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class LadyBugs {
  8.     public static void main(String[] args) throws IOException {
  9.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.         int fieldSize = Integer.parseInt(reader.readLine());
  11.         String [] indexes = reader.readLine().split(" ");
  12.         int[] field = new int[fieldSize];
  13.  
  14.         for (int i = 0; i < indexes.length; i++) {
  15.             int index = Integer.parseInt(indexes[i]);
  16.             if (index >= 0 && index < fieldSize){
  17.                 field[index] = 1;
  18.             }
  19.  
  20.         }
  21.  
  22.         String command = reader.readLine();
  23.  
  24.         while (!command.equals("end")){
  25.             String[] cmdArgs = command.split(" ");
  26.             int index = Integer.parseInt(cmdArgs[0]);
  27.             String cmd = cmdArgs[1];
  28.             int flyLength = Integer.parseInt(cmdArgs[2]);
  29.  
  30.             if (index < 0 || index > fieldSize - 1 || field[index] == 0){
  31.                 command = reader.readLine();
  32.                 continue;
  33.             }
  34.             field[index] = 0;
  35.             if (cmd.equals("right")) {
  36.                 index += flyLength;
  37.  
  38.                 while (index < fieldSize && field[index] == 1) {
  39.                     index += flyLength;
  40.                 }
  41.                 if (index < fieldSize) {
  42.                     field[index] = 1;
  43.                 }
  44.             } else {
  45.                 index -= flyLength;
  46.                 while (index >= 0 && field[index] == 1){
  47.                     index -= flyLength;
  48.                 }
  49.                 if (index >= 0){
  50.                     field[index] = 1;
  51.                 }
  52.             }
  53.  
  54.             command = reader.readLine();
  55.  
  56.         }
  57.  
  58.         for (int i = 0; i < field.length; i++) {
  59.             System.out.print(field[i] + " ");
  60.  
  61.         }
  62.         System.out.println();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement