desislava_topuzakova

10. LadyBugs

Oct 1st, 2021
1,683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.44 KB | None | 0 0
  1. package Arrays;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class demo {
  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(e -> Integer.parseInt(e)).toArray(); //индексите, на които има калинка
  14.  
  15.         for (int ladyBugIndex : ladyBugsIndexes) {
  16.             if(ladyBugIndex >= 0 && ladyBugIndex <= field.length - 1) {
  17.                 field[ladyBugIndex] = 1;
  18.             }
  19.         }
  20.  
  21.         String command = scanner.nextLine();
  22.  
  23.         while (!command.equals("end")) {
  24.             //command = "0 right 1".split(" ") ->
  25.             //0 от кой индекс се маха калинка
  26.             //right -> посока на движение
  27.             //1 с каква дължина се мести калинката
  28.             String[] tokens = command.split(" "); //["0", "right", "1"]
  29.             int index = Integer.parseInt(tokens[0]); //"0" -> 0
  30.             String direction = tokens[1];
  31.             int flyLength = Integer.parseInt(tokens[2]); //"1" -> 1
  32.  
  33.             //има летене ако: индексът е в масива и на индексът има калинка
  34.             if (index >= 0 && index <= field.length - 1 && field[index] == 1) {
  35.                 //калинката излетява
  36.                 field[index] = 0;
  37.                 if (direction.equals("right")) {
  38.                     index += flyLength; //нова позиция
  39.                     //ПРОВЕРКА ДАЛИ КАЦА ИЛИ ОЩЕ ЩЕ ЛЕТИ
  40.                     //дали новата позиция е свободна или е в полето
  41.                     //спира да лети: излезе извън полето или няма калинка на новата и позиция
  42.                     //продължава да лети:  ако е в полето и има калинка на новата и позиция
  43.                     while (index <= field.length - 1 && field[index] == 1) { //ако излетим или намерим празно място
  44.                         index += flyLength;
  45.                     }
  46.                     //СПИРАНЕ НА ЛЕТЕНЕ
  47.                     //ако не е излетяла
  48.                     if (index <= field.length - 1) {
  49.                         field[index] = 1;
  50.                         //каца
  51.                     }
  52.                 } else if (direction.equals("left")) {
  53.                     index -= flyLength;
  54.                     //спира да лети: index < 0  или  на index няма калиннка
  55.                     //продължава: index >= 0 && на индекса да има има калинка
  56.                     while (index >= 0 && field[index] == 1) { //ако не намерим празно място или излетим
  57.                         index -= flyLength;
  58.                     }
  59.                     if (index >= 0) {
  60.                         field[index] = 1;
  61.                     }
  62.                 }
  63.             }
  64.  
  65.  
  66.             command = scanner.nextLine();
  67.         }
  68.  
  69.         for (int number : field) {
  70.             System.out.print(number + " ");
  71.  
  72.         }
  73.  
  74.  
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment