deyanmalinov

7. Math Potato

Mar 23rd, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         String[] line = scan.nextLine().split("\\s+");
  8.         int num = Integer.parseInt(scan.nextLine());
  9.         ArrayDeque<String> queue = new ArrayDeque<>();
  10.         int prime = 1;
  11.         for (int i = 0; i < line.length; i++) {
  12.             queue.offer(line[i]);
  13.         }
  14.         while (queue.size() > 1){
  15.                 for (int i = 0; i < num-1; i++) {
  16.                     String name = queue.poll();
  17.                     queue.offer(name);
  18.                 }
  19.                 if (isPrime(prime)){
  20.                     System.out.println("Prime "+queue.peek());
  21.                 }else {
  22.                     System.out.println("Removed "+queue.poll());
  23.                  }
  24.                 prime++;
  25.         }
  26.             for (String s : queue) {
  27.             System.out.println("Last is " + s);
  28.         }
  29.     }
  30.     public static boolean isPrime(int num){
  31.         if (num ==1)return false;
  32.         for (int i = 2; i < num; i++) {
  33.             if (num%i == 0){
  34.                 return false;
  35.             }
  36.         }
  37.         return true;
  38.     }
  39. }
Add Comment
Please, Sign In to add comment