-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephus.java
More file actions
38 lines (30 loc) · 869 Bytes
/
Josephus.java
File metadata and controls
38 lines (30 loc) · 869 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
package fr.sofiane.programme;
import java.util.ArrayList;
import java.util.List;
public class Josephus {
public static <T> List<T> josephusPermutation(final List<T> items, final int k) {
int tailleInitiale=items.size();
List<T> retour=new ArrayList<T>();
List<T> temp=items;
int i=0;
while(retour.size()<tailleInitiale) {
if(i%k==k-1) {
retour.add(temp.get(i%temp.size()));
}else {
temp.add(temp.get(i%temp.size()));
}
i++;
}
return retour;
}
public static void main(String[] args) {
int[] tab = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<Integer> items= new ArrayList<Integer>();
for(int e:tab)
items.add(e);
for (int elem : josephusPermutation(items, 1))
{
System.out.println(elem);
}
}
}