Advertisement
desislava_topuzakova

3. Heart Delivery (List)

Jun 14th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.Arrays;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.stream.Collectors;
  7.  
  8. public class ThirdTask {
  9. public static void main(String[] args) {
  10. Scanner scanner = new Scanner(System.in);
  11.  
  12. //входни данни
  13. List<Integer> houses = Arrays.stream(scanner.nextLine() //"10@10@10@2"
  14. .split("@")) //["10", "10", "10", "2"]
  15. .map(Integer::parseInt) //[10, 10, 10, 2]
  16. .collect(Collectors.toList());
  17.  
  18. int currentIndex = 0; //на коя къща се намира Купидон
  19.  
  20. String command = scanner.nextLine();
  21.  
  22. while (!command.equals("Love!")) {
  23. //command = "Jump 3".split(" ") -> ["Jump", "3"]
  24. int jumpLength = Integer.parseInt(command.split(" ")[1]);
  25. //скача
  26. currentIndex += jumpLength;
  27.  
  28. //проверка дали е извън квартала
  29. if (currentIndex > houses.size() - 1) {
  30. currentIndex = 0;
  31. }
  32.  
  33. //къщата, в която отива -> проверка има ли нужда -> дава сърца
  34. if (houses.get(currentIndex) != 0) { //имаме нужда от сърца, защото бр. нужни сърца != 0
  35. houses.set(currentIndex, houses.get(currentIndex) - 2);
  36. //ако след даването вече нямаме нужда
  37. if (houses.get(currentIndex) == 0) {
  38. System.out.printf("Place %d has Valentine's day.%n", currentIndex);
  39. }
  40. } else {
  41. //къщата, в която отива -> брой нужни сърца = 0
  42. System.out.printf("Place %d already had Valentine's day.%n", currentIndex);
  43. }
  44. command = scanner.nextLine();
  45. }
  46.  
  47. //къща = 0 -> няма нужда да и даваме -> може да празнува празника
  48. //къща != 0 -> има нужда от даване на сърца
  49. System.out.printf("Cupid's last position was %d.%n", currentIndex);
  50. //успешна мисия -> всички стойности на къщите да са 0
  51. boolean isFailed = false;
  52. //isFailed = true -> провал
  53. //isFailed = false -> успешна
  54. int countFailedHouse = 0;
  55. for (int house : houses) {
  56. if (house != 0) {
  57. isFailed = true;
  58. countFailedHouse++;
  59. }
  60. }
  61. //isFailed = false -> преминали през всички къщи = 0
  62. if (isFailed) {
  63. System.out.printf("Cupid has failed %d places.%n", countFailedHouse);
  64. } else {
  65. //isFailed = false
  66. System.out.println("Mission was successful.");
  67. }
  68. }
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement