Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* a) Idee in Worten
- * Wir gehen durch die Queue, zaehlen auf, und jedes Mal, wenn der
- * Counter 0 modulo der Sylnumber, wird er aussortiert,
- * sonst wird er wieder eingereiht
- */
- import aud.Queue;
- public class JosephusProblem {
- @SuppressWarnings({"rawtypes", "unchecked"})
- public static <T> Queue<T> josephus(T[] children, int numbSyl) {
- // TODO: implementation
- Queue chosenOnes = new Queue();
- Queue youareout = new Queue();
- int i=0;
- while(i<children.length){
- chosenOnes.enqueue(children[i]);
- ++i;
- }
- int j=0;
- while(j<numbSyl){
- for(int counter = 1; counter<=numbSyl; ++counter){
- if (counter % numbSyl == 0) {
- youareout.enqueue(chosenOnes.dequeue());
- j += 1;
- } else {
- chosenOnes.enqueue(chosenOnes.dequeue());
- }
- }
- }
- return youareout;
- }
- public static void main(String[] args) {
- // TODO: test
- String[] test = {"anna", "bob", "clara", "david"};
- String[] c = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};
- System.out.println(josephus(test, 3));
- System.out.println(josephus(c, 7));
- }
- }
- //c) 1,2,3,4,5,6,7,8,9,10,11,12,13
- // 8,9,10,11,12,13,1,2,3,4,5,6
- // 2,3,4,5,6,8,9,10,11,12,13
- // 10,11,12,13,2,3,4,5,6,8
- // 5,6,8,10,11,12,13,2,3
- // 2,3,5,6,8,10,11,12
- // 12,2,3,5,6,8,10
- // 12,2,3,5,6,8,12
- // Und uebrig bleiben 2,3,5,6,8,12 und das sind die Stellen, an denen sich
- // die 6 stellen muessen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement