Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Cupid {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read the input string and split it by "@"
- String[] input = scanner.nextLine().split("@");
- // Convert the string array to an int array
- int[] neighborhood = Arrays.stream(input).mapToInt(Integer::parseInt).toArray();
- // Initialize the index of Cupid's position
- int index = 0;
- // Read the jump commands until "Love!"
- String command = scanner.nextLine();
- while (!command.equals("Love!")) {
- // Extract the jump length from the command
- int length = Integer.parseInt(command.split(" ")[1]);
- // Update the index by adding the length
- index += length;
- // If the index is outside the neighborhood, start from the first house
- if (index >= neighborhood.length) {
- index = 0;
- }
- // Decrease the needed hearts by 2
- neighborhood[index] -= 2;
- // Check if the house has Valentine's day or already had it
- if (neighborhood[index] == 0) {
- System.out.println("Place " + index + " has Valentine's day.");
- } else if (neighborhood[index] < 0) {
- System.out.println("Place " + index + " already had Valentine's day.");
- }
- // Read the next command
- command = scanner.nextLine();
- }
- // Print Cupid's final position
- System.out.println("Cupid's last position was " + index + ".");
- // Count how many houses are successful and how many are not
- int successful = 0;
- int failed = 0;
- for (int house : neighborhood) {
- if (house <= 0) {
- successful++;
- } else {
- failed++;
- }
- }
- // Print the result
- if (failed == 0) {
- System.out.println("Mission was successful.");
- } else {
- System.out.println("Cupid has failed " + failed + " places.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement