Advertisement
tripTiPscout

07. Math Potato

May 22nd, 2023
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. package advanced;
  2.  
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] children = scanner.nextLine().split(" ");
  11.         int toss = Integer.parseInt(scanner.nextLine());
  12.  
  13.         PriorityQueue<String> queue = new PriorityQueue<>();
  14.  
  15.         for (String child : children) {
  16.             queue.offer(child);
  17.         }
  18.  
  19.         int cycle = 1;
  20.  
  21.         while (queue.size() > 1) {
  22.             for (int i = 1; i < toss; i++) {
  23.                 queue.offer(Objects.requireNonNull(queue.poll()));
  24.             }
  25.             if (isPrime(cycle)) {
  26.                 System.out.println("Prime " + queue.peek());
  27.             } else {
  28.                 System.out.println("Removed " + queue.poll());
  29.             }
  30.             cycle++;
  31.         }
  32.  
  33.         System.out.println("Last is " + queue.poll());
  34.  
  35.     }
  36.  
  37.     private static boolean isPrime(int cycle) {
  38.         boolean isPrime = true;
  39.         for (int i = 2; i <= cycle - 1; i++) {
  40.             if (cycle % i == 0) {
  41.                 isPrime = false;
  42.                 break;
  43.             }
  44.         }
  45.         if (cycle == 1) {
  46.             return false;
  47.         }
  48.         return isPrime;
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement