Advertisement
steef_br

#LadyBugs,#Fundamentals#Arrays

Apr 28th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package FundamentalsModule.ArraysExercises;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class LadyBugs {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. int fieldSize = Integer.parseInt(scanner.nextLine());
  11. int[] field = new int[fieldSize];
  12. int[] ladyBugs = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  13.  
  14.  
  15. for (int Bug : ladyBugs) {
  16. if (Bug >= 0 && Bug < field.length) {
  17. field[Bug] = 1;
  18. }
  19. }
  20.  
  21. String input = scanner.nextLine();
  22. while (!input.equals("end")) {
  23. String[] command = input.split(" ");
  24. int insectIndex = Integer.parseInt(command[0]);
  25. String direction = command[1];
  26. int flyLength = Integer.parseInt(command[2]);
  27.  
  28. if (insectIndex >= 0 && insectIndex < field.length && field[insectIndex] > 0 && flyLength != 0) { // Validaciq za index samo v ochertaniqta na kutiqta
  29. field[insectIndex] = 0;
  30. int flight = flyLength;
  31. switch (direction) {
  32. case "right":
  33.  
  34. while (insectIndex + flight < field.length && insectIndex + flight >= 0 && field[insectIndex + flight] == 1) {
  35. flight++;
  36. }
  37. field[insectIndex + flight] = 1;
  38. break;
  39.  
  40.  
  41. case "left":
  42.  
  43. while (insectIndex - flight >= 0 && insectIndex - flight < field.length && field[insectIndex - flight] == 1) {
  44. flight++;
  45. }
  46. field[insectIndex - flight] = 1;
  47. break;
  48.  
  49. }
  50. }
  51.  
  52.  
  53. input = scanner.nextLine();
  54. }
  55.  
  56. for(
  57. int index :
  58. field)
  59.  
  60. {
  61. System.out.print(index + " ");
  62. }
  63. }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement