Advertisement
ingwey

dsc<

Apr 13th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1.  
  2. /* a) Idee in Worten
  3. We go through the queue, count up, and every time our counter is 0 modulo our
  4. Sylnumber, the one has to leave, otherwise he gets enqueued again
  5. */
  6. import aud.Queue;
  7.  
  8. public class Josephus2 {
  9.  
  10. @SuppressWarnings({ "rawtypes", "unchecked" })
  11. public static <T> Queue<T> josephus(T[] children, int numbSyl) {
  12. // TODO: implementation
  13. Queue chosenOnes=new Queue();
  14. Queue youareout=new Queue();
  15. for (int i=0;i<children.length;++i) {
  16. chosenOnes.enqueue(children[i]);
  17. }
  18. 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
  19. if (zähler % numbSyl ==0) {
  20. youareout.enqueue(chosenOnes.dequeue());
  21. i+=1;
  22. }
  23. else {
  24. chosenOnes.enqueue(chosenOnes.dequeue());
  25. }
  26. }
  27.  
  28. return chosenOnes;
  29. }
  30.  
  31.  
  32. public static void main(String[] args) {
  33. // TODO: test
  34. String[] test = {"anna","bob","clara","david"};
  35. String[] c = {"1","2","3","4","5","6","7","8","9","10","11","12","13"};
  36.  
  37. System.out.println(josephus(test,3));
  38. System.out.println(josephus(c,7));
  39. //c) 12<2<3<5<6<8<10<
  40. // 7 spots for six people.
  41. //seems like numbSyl is the number of people who should be chosen, at least failed submission implys it.
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement