CoineTre

JF-ExcBasic09. Lady Bug

Feb 2nd, 2021 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Exc10LadyBug {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         int size = Integer.parseInt(scanner.nextLine());
  7.         int[] field = new int[size];
  8.         String[] bugIndex = scanner.nextLine().split(" ");
  9.         for (int i = 0; i < bugIndex.length; i++) {
  10.             int bugPosition = Integer.parseInt(bugIndex[i]);
  11.             if (bugPosition >= 0 && bugPosition < field.length){
  12.                 field[bugPosition]= 1;
  13.             }
  14.         }
  15.         String line = scanner.nextLine();
  16.         while (!line.equals("end")){
  17.             String[] commands = line.split(" ");
  18.             int bugPosition = Integer.parseInt(commands[0]);
  19.             String direction = commands[1];
  20.             int length = Integer.parseInt(commands[2]);
  21.             if (bugPosition < 0 || bugPosition >= field.length || field[bugPosition] != 1){
  22.                 line = scanner.nextLine();
  23.                 continue;
  24.             }
  25.             field[bugPosition] = 0;
  26.             if (direction.equals("right")){
  27.                 bugPosition+=length;
  28.                 while(bugPosition< field.length && field[bugPosition] == 1){
  29.                     bugPosition+=length;
  30.                 }
  31.                 if (bugPosition < field.length){
  32.                     field[bugPosition]=1;
  33.                 }
  34.             }else {
  35.                 bugPosition-=length;
  36.                 while (bugPosition >= 0 && field[bugPosition] == 1){
  37.                     bugPosition -= length;
  38.                 }
  39.                 if (bugPosition >= 0){
  40.                     field[bugPosition]=1;
  41.                 }
  42.             }
  43.             line = scanner.nextLine();
  44.         }
  45.         for (int i = 0; i < field.length; i++) {
  46.             System.out.print(field[i] + " ");
  47.         }
  48.     }
  49. }
  50.  
  51. /*Lady Bug
  52. You are given a field size and the indexes of ladybugs inside the field. After that on every new line until the "end" command is given, a ladybug changes its position either to its left or to its right by a given fly length.
  53. A command to a ladybug looks like this: "0 right 1". This means that the little insect placed on index 0 should fly one index to its right. If the ladybug lands on a fellow ladybug, it continues to fly in the same direction by the same fly length. If the ladybug flies out of the field, it is gone.
  54. If you are given ladybug index that does not have ladybug there, nothing happens. If you are given ladybug index that is outside the field, nothing happens.
  55. Your job is to create the program, simulating the ladybugs flying around doing nothing. At the end, print all cells in the field separated by blank spaces. For each cell that has a ladybug on it print '1' and for each empty cells print '0'. For the example above, the output should be '0 1 0'.
  56. */
Add Comment
Please, Sign In to add comment