-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathPotato.java
52 lines (45 loc) · 1.61 KB
/
MathPotato.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package StacksAndQueues_Lab;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Scanner;
public class MathPotato {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String kids [] = scan.nextLine().split("\\s+");
int n = Integer.parseInt(scan.nextLine());
ArrayDeque <String> queue = new ArrayDeque<>();
Collections.addAll(queue,kids);
int count = 0;
while(queue.size()>1) {
count++;
boolean isPrime = true;
for (int i = 1; i < n; i++) { //2
String currentKid = queue.poll();
queue.offer(currentKid); // MARIA PETER GEORGE - maria george
}
if (count == 1) {
isPrime = false;
}
int countPrime = 0;
for (int j = 1; j <= count; j++) {
if (count % j == 0) {
countPrime++;
}
}
if (countPrime == 2){
isPrime = true;
}else{
isPrime = false;
}
if (isPrime) {
// String luckyKid = queue.poll();
System.out.println("Prime " + queue.peek());
// queue.offer(luckyKid);
} else {
//false
System.out.println("Removed " + queue.poll());
}
}
System.out.println("Last is " + queue.peek());
}
}