emodev

Untitled

Dec 21st, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package LinearDataStructures.Lab_StartSeriously_21dec;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayDeque;
  7. import java.util.Collections;
  8.  
  9. public class e09_MathPotato {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.         String[] names = reader.readLine().split(" ");
  13.         int num = Integer.parseInt(reader.readLine());
  14.  
  15.  
  16.         ArrayDeque<String> children = new ArrayDeque<>();
  17.         Collections.addAll(children, names);
  18.         int cycle = 1;
  19.         while (children.size() > 1) {
  20.             for (int i = 1; i < num; i++) {
  21.                 children.offer(children.poll());
  22.             }
  23.             if (isPrimeNumber(cycle)) {
  24.                 System.out.println("Prime " + children.peek());
  25.             } else {
  26.                 System.out.println("Removed " + children.poll());
  27.             }
  28.             cycle++;
  29.         }
  30.  
  31.  
  32.         System.out.println("Last is " + children.poll());
  33.  
  34.     }
  35.  
  36.     public static boolean isPrimeNumber(int i) {
  37.         int factors = 0;
  38.         int j = 1;
  39.  
  40.         while (j <= i) {
  41.             if (i % j == 0) {
  42.                 factors++;
  43.             }
  44.             j++;
  45.         }
  46.         return (factors == 2);
  47.     }
  48. }
Add Comment
Please, Sign In to add comment