File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
1-Fundamentals/1-3-BagsQueuesStacks Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ /*************************************************************************
3+ *
4+ * Josephus problem
5+ *
6+ * % java Ex_1_3_37 7 2
7+ * 1 3 5 0 4 2 6
8+ *
9+ *************************************************************************/
10+
11+ public class Ex_1_3_37
12+ {
13+ public static void main (String [] args )
14+ {
15+ int n = Integer .parseInt (args [0 ]),
16+ m = Integer .parseInt (args [1 ]);
17+
18+ Queue <Integer > q = new Queue <Integer >();
19+ for (int i = 0 ; i < n ; i ++)
20+ q .enqueue (new Integer (i ));
21+
22+ int k = 0 ;
23+ while (!q .isEmpty ())
24+ {
25+ int x = q .dequeue ();
26+
27+ if (++k % m == 0 )
28+ StdOut .print (x + " " );
29+ else
30+ q .enqueue (x );
31+ }
32+ StdOut .println ();
33+ }
34+ }
You can’t perform that action at this time.
0 commit comments