From 06703c1371ffdf669d3996baa9e9684d13cdcc82 Mon Sep 17 00:00:00 2001 From: Adrian Istrate Date: Sun, 25 Dec 2011 23:45:31 +0100 Subject: [PATCH] Josephus problem (Ex. 1.3.37). --- .../1-3-BagsQueuesStacks/Ex_1_3_37.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_37.java diff --git a/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_37.java b/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_37.java new file mode 100644 index 0000000..6351b36 --- /dev/null +++ b/1-Fundamentals/1-3-BagsQueuesStacks/Ex_1_3_37.java @@ -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 q = new Queue(); + 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(); + } +}