Didart

Math Potato

Dec 27th, 2022
925
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. package StacksAndQueues;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Scanner;
  5.  
  6. public class MathPotato {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] input = scanner.nextLine().split("\\s+");
  11.         int number = Integer.parseInt(scanner.nextLine());
  12.  
  13.         ArrayDeque<String> queue = new ArrayDeque<>();
  14.  
  15.         int cycle = 1;
  16.         for (int index = 0; index < input.length; index++) {
  17.             queue.offer(input[index]);
  18.         }
  19.         while (queue.size() > 1) {
  20.  
  21.             for (int i = 0; i < number - 1; i++) {
  22.                 String name = queue.poll();
  23.                 queue.offer(name);
  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.         for (String kid : queue) {
  33.             System.out.println("Last is " + kid);
  34.         }
  35.     }
  36.  
  37.     public static boolean isPrime(int number) {
  38.         if (number == 1) return false;
  39.        
  40.         for (int i = 2; i < number; i++) {
  41.             if (number % i == 0) {
  42.                 return false;
  43.             }
  44.         }
  45.         return true;
  46.     }
  47.    
  48. }
Advertisement
Add Comment
Please, Sign In to add comment