Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Actual_Mid_Exam;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- public class Task1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- List<Integer> houses = new ArrayList<>(Arrays.stream(scanner.nextLine().split("@"))
- .map(Integer::parseInt)
- .collect(Collectors.toList()));
- String input;
- int index = 0;
- int currentPosition = 0;
- while (!"Love!".equals(input = scanner.nextLine())) {
- String[] tokens = input.split(" ");
- int length = Integer.parseInt(tokens[1]);
- index = jump(houses, length, index);
- if (houses.get(index) == 0) {
- System.out.printf("Place %d already had Valentine's day.\n", index);
- } else {
- houses.set(index, houses.get(index) - 2);
- if (houses.get(index) == 0) {
- System.out.printf("Place %d has Valentine's day.\n", index);
- }
- }
- }
- int failedHouses = 0;
- System.out.println("Cupid's last position was " + index + ".");
- for (int i = 0; i < houses.size(); i++) {
- if (houses.get(i) != 0) {
- failedHouses++;
- }
- }
- if (failedHouses == 0) {
- System.out.println("Mission was successful.");
- } else {
- System.out.printf("Cupid has failed %d places.", failedHouses);
- }
- }
- private static int jump(List<Integer> houses, int length, int currentPosition) {
- int index = currentPosition;
- for (int i = 0; i < length; i++) {
- index++;
- if (index == houses.size()) {
- return index = 0;
- }
- }
- return index;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement