Advertisement
ingwey

sdcav

Apr 14th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1.  
  2. /* a) Idee in Worten
  3. * Wir gehen durch die Queue, zaehlen auf, und jedes Mal, wenn der
  4. * Counter 0 modulo der Sylnumber, wird er aussortiert,
  5. * sonst wird er wieder eingereiht
  6. */
  7. import aud.Queue;
  8.  
  9. public class JosephusProblem {
  10.  
  11. @SuppressWarnings({"rawtypes", "unchecked"})
  12. public static <T> Queue<T> josephus(T[] children, int numbSyl) {
  13. // TODO: implementation
  14. Queue chosenOnes = new Queue();
  15. Queue youareout = new Queue();
  16.  
  17. int i=0;
  18. while(i<children.length){
  19. chosenOnes.enqueue(children[i]);
  20. ++i;
  21. }
  22.  
  23. int j=0;
  24. while(j<numbSyl){
  25. for(int counter = 1; counter<=numbSyl; ++counter){
  26. if (counter % numbSyl == 0) {
  27. youareout.enqueue(chosenOnes.dequeue());
  28. j += 1;
  29. } else {
  30. chosenOnes.enqueue(chosenOnes.dequeue());
  31. }
  32. }
  33. }
  34.  
  35. return youareout;
  36. }
  37.  
  38. public static void main(String[] args) {
  39. // TODO: test
  40. String[] test = {"anna", "bob", "clara", "david"};
  41. String[] c = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"};
  42.  
  43. System.out.println(josephus(test, 3));
  44. System.out.println(josephus(c, 7));
  45. }
  46. }
  47.  
  48. //c) 1,2,3,4,5,6,7,8,9,10,11,12,13
  49. // 8,9,10,11,12,13,1,2,3,4,5,6
  50. // 2,3,4,5,6,8,9,10,11,12,13
  51. // 10,11,12,13,2,3,4,5,6,8
  52. // 5,6,8,10,11,12,13,2,3
  53. // 2,3,5,6,8,10,11,12
  54. // 12,2,3,5,6,8,10
  55. // 12,2,3,5,6,8,12
  56.  
  57. // Und uebrig bleiben 2,3,5,6,8,12 und das sind die Stellen, an denen sich
  58. // die 6 stellen muessen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement