Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Josephus-problem/Java/josephus-problem-1.java
Normal file
40
Task/Josephus-problem/Java/josephus-problem-1.java
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import java.util.ArrayList;
|
||||
|
||||
public class Josephus {
|
||||
public static int execute(int n, int k){
|
||||
int killIdx = 0;
|
||||
ArrayList<Integer> prisoners = new ArrayList<Integer>(n);
|
||||
for(int i = 0;i < n;i++){
|
||||
prisoners.add(i);
|
||||
}
|
||||
System.out.println("Prisoners executed in order:");
|
||||
while(prisoners.size() > 1){
|
||||
killIdx = (killIdx + k - 1) % prisoners.size();
|
||||
System.out.print(prisoners.get(killIdx) + " ");
|
||||
prisoners.remove(killIdx);
|
||||
}
|
||||
System.out.println();
|
||||
return prisoners.get(0);
|
||||
}
|
||||
|
||||
public static ArrayList<Integer> executeAllButM(int n, int k, int m){
|
||||
int killIdx = 0;
|
||||
ArrayList<Integer> prisoners = new ArrayList<Integer>(n);
|
||||
for(int i = 0;i < n;i++){
|
||||
prisoners.add(i);
|
||||
}
|
||||
System.out.println("Prisoners executed in order:");
|
||||
while(prisoners.size() > m){
|
||||
killIdx = (killIdx + k - 1) % prisoners.size();
|
||||
System.out.print(prisoners.get(killIdx) + " ");
|
||||
prisoners.remove(killIdx);
|
||||
}
|
||||
System.out.println();
|
||||
return prisoners;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
System.out.println("Survivor: " + execute(41, 3));
|
||||
System.out.println("Survivors: " + executeAllButM(41, 3, 3));
|
||||
}
|
||||
}
|
||||
37
Task/Josephus-problem/Java/josephus-problem-2.java
Normal file
37
Task/Josephus-problem/Java/josephus-problem-2.java
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Josephus {
|
||||
|
||||
public static void main(String[] args) {
|
||||
execute(5, 1);
|
||||
execute(41, 2);
|
||||
execute(23482, 3342, 3);
|
||||
}
|
||||
|
||||
public static int[][] execute(int n, int k) {
|
||||
return execute(n, k, 1);
|
||||
}
|
||||
|
||||
public static int[][] execute(int n, int k, int s) {
|
||||
List<Integer> ps = new ArrayList<Integer>(n);
|
||||
for (int i=0; i<n; i+=1) ps.add(i);
|
||||
List<Integer> ks = new ArrayList<Integer>(n-s);
|
||||
for (int i=k; ps.size()>s; i=(i+k)%ps.size()) ks.add(ps.remove(i));
|
||||
System.out.printf("Josephus(%d,%d,%d) -> %s / %s\n", n, k, s, toString(ps), toString(ks));
|
||||
return new int[][] {
|
||||
ps.stream().mapToInt(Integer::intValue).toArray(),
|
||||
ks.stream().mapToInt(Integer::intValue).toArray()
|
||||
};
|
||||
}
|
||||
|
||||
private static String toString(List <Integer> ls) {
|
||||
String dot = "";
|
||||
if (ls.size() >= 45) {
|
||||
dot = ", ...";
|
||||
ls = ls.subList(0, 45);
|
||||
}
|
||||
String s = ls.toString();
|
||||
return s.substring(1, s.length()-1) + dot;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue