Advertisement
psi_mmobile

Untitled

Oct 22nd, 2022
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.93 KB | None | 0 0
  1. public class MyClass {
  2.     public static void main(String args[]) {
  3.         java.util.Scanner scanner = new java.util.Scanner(System.in);
  4.         String[] houses = scanner.nextLine().split("@");
  5.         int[] house = new int[houses.length];
  6.         for(int i = 0;i < houses.length;i++) {
  7.            house[i] = Integer.parseInt(houses[i]);
  8.         }
  9.         int modCap = houses.length -1;
  10.         int lastPosition = 0;
  11.         String nextLine = null;
  12.         int houseIndex = 0;
  13.         while(true) {
  14.             nextLine = scanner.nextLine();
  15.             if (nextLine.equals("Love!")) {
  16.                 break;
  17.             } else {
  18.                 String[] jumpIndex = nextLine.split(" ");
  19.                 int jumpIdx = Integer.parseInt(jumpIndex[1]);
  20.                 // houseIndex = (houseIndex + jumpIdx) % modCap;
  21.                 houseIndex += jumpIdx;
  22.                 if (houseIndex > modCap) {
  23.                     houseIndex = 0;
  24.                 }
  25.                 if (house[houseIndex] != 0) {
  26.                     house[houseIndex] -= 2;
  27.                     if (house[houseIndex] == 0) {
  28.                         System.out.printf("Place %d has Valentine's day.\n",houseIndex);
  29.                     }
  30.                 } else {
  31.                     System.out.printf("Place %d already had Valentine's day.\n",houseIndex);
  32.                 }
  33.                 lastPosition = houseIndex;
  34.             }
  35.         }
  36.         System.out.printf("Cupid's last position was %d.\n",lastPosition);
  37.         if (failsCount(house) == 0) {
  38.             System.out.println("Mission was successful.");
  39.         } else {
  40.             System.out.printf("Cupid has failed %d places.",failsCount(house));
  41.         }
  42.     }
  43.     public static int failsCount(int[] houses) {
  44.         int fails = 0;
  45.         for(int i = 0; i < houses.length;i++) {
  46.             if (houses[i] > 0) {
  47.                 fails++;
  48.             }
  49.         }
  50.         return fails;
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement