Advertisement
Guest User

Untitled

a guest
Feb 29th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. package Actual_Mid_Exam;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. import java.util.Scanner;
  7. import java.util.stream.Collectors;
  8.  
  9. public class Task1 {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12.  
  13. List<Integer> houses = new ArrayList<>(Arrays.stream(scanner.nextLine().split("@"))
  14. .map(Integer::parseInt)
  15. .collect(Collectors.toList()));
  16.  
  17. String input;
  18. int index = 0;
  19. int currentPosition = 0;
  20. while (!"Love!".equals(input = scanner.nextLine())) {
  21. String[] tokens = input.split(" ");
  22. int length = Integer.parseInt(tokens[1]);
  23. index = jump(houses, length, index);
  24.  
  25. if (houses.get(index) == 0) {
  26. System.out.printf("Place %d already had Valentine's day.\n", index);
  27. } else {
  28. houses.set(index, houses.get(index) - 2);
  29. if (houses.get(index) == 0) {
  30. System.out.printf("Place %d has Valentine's day.\n", index);
  31. }
  32. }
  33.  
  34. }
  35. int failedHouses = 0;
  36. System.out.println("Cupid's last position was " + index + ".");
  37. for (int i = 0; i < houses.size(); i++) {
  38. if (houses.get(i) != 0) {
  39. failedHouses++;
  40. }
  41. }
  42. if (failedHouses == 0) {
  43. System.out.println("Mission was successful.");
  44. } else {
  45. System.out.printf("Cupid has failed %d places.", failedHouses);
  46. }
  47. }
  48.  
  49. private static int jump(List<Integer> houses, int length, int currentPosition) {
  50. int index = currentPosition;
  51. for (int i = 0; i < length; i++) {
  52. index++;
  53. if (index == houses.size()) {
  54. return index = 0;
  55. }
  56. }
  57. return index;
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement