Skip to content

Commit

Permalink
Josephus problem (Ex. 1.3.37).
Browse files Browse the repository at this point in the history
  • Loading branch information
aistrate committed Dec 25, 2011
1 parent af0cd72 commit 06703c1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_37.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

/*************************************************************************
*
* Josephus problem
*
* % java Ex_1_3_37 7 2
* 1 3 5 0 4 2 6
*
*************************************************************************/

public class Ex_1_3_37
{
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]),
m = Integer.parseInt(args[1]);

Queue<Integer> q = new Queue<Integer>();
for (int i = 0; i < n; i++)
q.enqueue(new Integer(i));

int k = 0;
while (!q.isEmpty())
{
int x = q.dequeue();

if (++k % m == 0)
StdOut.print(x + " ");
else
q.enqueue(x);
}
StdOut.println();
}
}

0 comments on commit 06703c1

Please sign in to comment.