算法(Algorithms)第4版 练习 1.3.37
package com.qiusongde.creative; import com.qiusongde.Queue; import edu.princeton.cs.algs4.StdOut; public class Josephus { public static void main(String[] args) { int n = Integer.parseInt(args[0]); int m = Integer.parseInt(args[1]); Queue<Integer> queue = new Queue<Integer>(); for(int i = 0; i < n; i++) { queue.enqueue(i); } while(!queue.isEmpty()) { for(int i = 0; i < (m - 1); i++) { queue.enqueue(queue.dequeue()); } StdOut.print(queue.dequeue() + " "); } } }
测试1:
1 3 5 0 4 2 6
测试2:
2 6 5 1 3 0 4