-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathCircleGame.java
More file actions
31 lines (28 loc) · 825 Bytes
/
CircleGame.java
File metadata and controls
31 lines (28 loc) · 825 Bytes
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
package recursion;
import java.util.ArrayList;
import java.util.Scanner;
public class CircleGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of players:");
int n = sc.nextInt();
System.out.println("Enter the key:");
int k = sc.nextInt();
int ans = circle(n,k);
System.out.println("The winner is: "+ ans);
}
private static int circle(int n, int k) {
ArrayList<Integer> list = new ArrayList<>();
for (int i=1; i <= n; i++) {
list.add(i);
}
int i=1;
while (list.size()>1) {
for(int t=0; t<k; t++) {
i = (i+1)%n;
}
list.remove((i+1));
}
return list.get(0);
}
}