Advertisement
desislava_topuzakova

Untitled

Feb 21st, 2023
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Ex1 {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. String[] data = scanner.nextLine().split(" ");
  11. PriorityQueue<String> queue = new PriorityQueue<>();
  12. Collections.addAll(queue, data);
  13.  
  14. int n = Integer.parseInt(scanner.nextLine());
  15. int cycle = 1;
  16. while (queue.size() > 1) {
  17. for (int i = 1; i < n; i++) {
  18. queue.offer(queue.poll());
  19. }
  20. if (isPrime(cycle)) {
  21. System.out.println("Prime " + queue.peek());
  22. } else {
  23.  
  24. System.out.println("Removed " + queue.poll());
  25. }
  26. cycle++;
  27. }
  28.  
  29. System.out.println("Last is " + queue.poll());
  30. }
  31.  
  32. static boolean isPrime(int cycle) {
  33. if (cycle <= 1)
  34. return false;
  35. else if (cycle == 2)
  36. return true;
  37. else if (cycle % 2 == 0)
  38. return false;
  39. for (int i = 3; i <= Math.sqrt(cycle); i += 2) {
  40. if (cycle % i == 0)
  41. return false;
  42. }
  43. return true;
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement