MartinPaunov

Lady Bug

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