Advertisement
jules0707

Josephus

Dec 15th, 2017
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.32 KB | None | 0 0
  1. /* From https://algs4.cs.princeton.edu/13stacks/Josephus.java.html
  2. * Josephus problem. In the Josephus problem from antiquity, N people are in dire straits and agree to the
  3. * following strategy to reduce the population. They arrange themselves in a circle (at positions numbered
  4. * from 0 to N-1) and proceed around the circle, eliminating every Mth person until only one person is left.
  5. * Legend has it that Josephus figured out where to sit to avoid being eliminated. Write a Queue client
  6. * Josephus.java that takes M and N from the command line and prints out the order in which people are
  7. * eliminated (and thus would show Josephus where to sit in the circle).*/
  8.  
  9. import edu.princeton.cs.algs4.StdIn;
  10. import edu.princeton.cs.algs4.StdOut;
  11.  
  12. public class Josephus {
  13.     public static void main(String[] args) {
  14.         int m = StdIn.readInt();
  15.         int n = StdIn.readInt();
  16.  
  17.         // initialize the queue
  18.         LinkedQueueOfIntegers queue = new LinkedQueueOfIntegers();
  19.         for (int i = 0; i < n; i++)
  20.             queue.enqueue(i);
  21.  
  22.         while (!queue.isEmpty()) {
  23.             for (int i = 0; i < m - 1; i++)
  24.              // we send back at the beginning of the queue the first mth positions using dequeue with
  25.              // and print the position that goes away.
  26.                 queue.enqueue(queue.dequeue());
  27.                 StdOut.print(queue.dequeue()+ " ");
  28.         }
  29.         StdOut.println();
  30.     }
  31. }
  32.  
  33. ---------------with this implementation of a Queue of Integers using Linked-list for example -------------
  34.  
  35. public class LinkedQueueOfIntegers {
  36.  
  37.     private Node first, last; // we maintain 2 pointers in a linked list for queues
  38.    
  39.     private class Node {  // a nested inner class to get the recursion feature
  40.         Integer item;
  41.         Node next;
  42.     }
  43.    
  44.     public boolean isEmpty() {return first == null;}
  45.    
  46.    
  47.     public void enqueue(Integer item ){
  48.         Node oldLast = last;    // we save a copy of the current last item
  49.         last = new Node(); // initialize it with a new node
  50.         last.item = item; // and populate it
  51.         last.next = null;
  52.         if(isEmpty()) first = last; // case of empty list
  53.         else oldLast.next = last; // and we reconnect the oldLast to point to the new last of the queue
  54.     }
  55.  
  56.     public Integer dequeue() {
  57.         Integer item = first.item; // we save the value of the current first item
  58.         first = first.next; // and reset the value of first to the next item
  59.         if(isEmpty()) last = null;
  60.         return item;
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement