Advertisement
Guest User

Untitled

a guest
Jan 30th, 2023
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class _10TreasureHunt {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.      
  8.         String input = scanner.nextLine();
  9.         String[] treasures = input.split("\\|");
  10.  
  11.         String command;
  12.         while (!"Yohoho!".equals(command = scanner.nextLine())) {
  13.                String[] treasureInput = command.split("\\s");
  14.             String treasureCmd = treasureInput[0];
  15.  
  16.             if ("Loot".equals(treasureCmd)) {
  17.                 for (int i = 1; i < treasureInput.length; i++) {
  18.                     boolean isItemExisting = false;
  19.  
  20.                     for (String treasure : treasures) {
  21.                         if (treasureInput[i].equals(treasure)) {
  22.                             isItemExisting = true;
  23.                             break;
  24.                         }
  25.                     }
  26.  
  27.                     if (!isItemExisting) {
  28.                         String[] treasuresNew = new String[treasures.length + 1];
  29.                         treasuresNew[0] = treasureInput[i];
  30.                         System.arraycopy(treasures, 0, treasuresNew, 1, treasures.length);
  31.                         treasures = treasuresNew;
  32.                     }
  33.                 }
  34.            } else if ("Drop".equals(treasureCmd)) {
  35.                 int index = Integer.parseInt(treasureInput[1]);
  36.  
  37.                 if (index >= 0 && index <= treasures.length - 1) {
  38.                     String droppedLoot = treasures[index];
  39.  
  40.                     for (int i = index; i < treasures.length - 1; i++) {
  41.                         treasures[i] = treasures[i + 1];
  42.                     }
  43.  
  44.                     treasures[treasures.length - 1] = droppedLoot;
  45.                 }
  46.             } else if ("Steal".equals(treasureCmd)) {
  47.                 int items = Integer.parseInt(treasureInput[1]);
  48.                 int itemsToDrop = Math.min(items, treasures.length);
  49.                 if (itemsToDrop > 0) {
  50.                     String[] stolenTreasures = Arrays.copyOfRange(treasures, treasures.length - itemsToDrop, treasures.length);
  51.                     treasures = Arrays.copyOf(treasures, treasures.length - itemsToDrop);
  52.                     System.out.println(String.join(", ", stolenTreasures));
  53.                 }
  54.             }
  55.         }
  56.      
  57.         if (treasures.length > 0) {
  58.             int sumItems = 0;
  59.             for (String treasure : treasures) {
  60.                 sumItems += treasure.length();
  61.             }
  62.             double avgTreasureGain = sumItems * 1.0 / treasures.length;
  63.  
  64.             System.out.printf("Average treasure gain: %.2f pirate credits.", avgTreasureGain);
  65.         } else {
  66.             System.out.println("Failed treasure hunt.");
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement