Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* a) Idee in Worten
- We go through the queue, count up, and every time our counter is 0 modulo our
- Sylnumber, the one has to leave, otherwise he gets enqueued again
- */
- import aud.Queue;
- public class Josephus2 {
- @SuppressWarnings({ "rawtypes", "unchecked" })
- public static <T> Queue<T> josephus(T[] children, int numbSyl) {
- // TODO: implementation
- Queue chosenOnes=new Queue();
- Queue youareout=new Queue();
- for (int i=0;i<children.length;++i) {
- chosenOnes.enqueue(children[i]);
- }
- for(int i=0, zähler=1;i<(children.length-numbSyl);++zähler) { //i<x where x is the amount of people who should be chosen
- if (zähler % numbSyl ==0) {
- youareout.enqueue(chosenOnes.dequeue());
- i+=1;
- }
- else {
- chosenOnes.enqueue(chosenOnes.dequeue());
- }
- }
- return chosenOnes;
- }
- 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) 12<2<3<5<6<8<10<
- // 7 spots for six people.
- //seems like numbSyl is the number of people who should be chosen, at least failed submission implys it.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement