Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class Ex1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] data = scanner.nextLine().split(" ");
- PriorityQueue<String> queue = new PriorityQueue<>();
- Collections.addAll(queue, data);
- int n = Integer.parseInt(scanner.nextLine());
- int cycle = 1;
- while (queue.size() > 1) {
- for (int i = 1; i < n; i++) {
- queue.offer(queue.poll());
- }
- if (isPrime(cycle)) {
- System.out.println("Prime " + queue.peek());
- } else {
- System.out.println("Removed " + queue.poll());
- }
- cycle++;
- }
- System.out.println("Last is " + queue.poll());
- }
- static boolean isPrime(int cycle) {
- if (cycle <= 1)
- return false;
- else if (cycle == 2)
- return true;
- else if (cycle % 2 == 0)
- return false;
- for (int i = 3; i <= Math.sqrt(cycle); i += 2) {
- if (cycle % i == 0)
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement