forked from CodeNerve/Let-Us-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephus_Problem.java
More file actions
42 lines (33 loc) · 990 Bytes
/
Josephus_Problem.java
File metadata and controls
42 lines (33 loc) · 990 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
32
33
34
35
36
37
38
39
40
41
42
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Josephus_Problem{
static boolean kill(List<Integer> list, boolean start){
int last = list.get(list.size()-1);
if(start){
for(int i=0;i< list.size()-1;i++){
list.remove(i+1);
}
}else{
for(int i=0;i<list.size();i++){
list.remove(i);
}
}
return last != list.get(list.size() - 1);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//System.out.println("Give n");
int n = scanner.nextInt();
List<Integer> list = new ArrayList<>(n);
for(int i=0;i<n;i++){
list.add(i+1);
}
boolean start = true;
while(list.size()!=1){
start = kill(list,start);
//System.out.println(list);
}
System.out.println(list.get(0));
}
}