-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoseph.java
More file actions
39 lines (37 loc) · 762 Bytes
/
Copy pathJoseph.java
File metadata and controls
39 lines (37 loc) · 762 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
package leon.algorithm;
public class Joseph {
public static void main(String[] args) {
new Thread(new Runnable(){
public void run(){
joseph(30,5);
}
public void joseph(int n, int k){
int[] array = new int[n];
for(int i=0;i<n;i++){
array[i]=i+1;
}
int pointer = 0;
int accumulate=0;
int count = 0;
while(count<n){
if(array[pointer]!=0){
accumulate++;
if(n-count == 1){
System.out.print(array[pointer]+" ");
array[pointer]=0;
count++;
}
if(accumulate==k){
System.out.print(array[pointer]+" ");
array[pointer]=0;
accumulate=0;
count++;
}
}
pointer = (pointer+1)%n;
}
System.out.println();
}
}).start();
}
}