您的位置:首页 > 其它

算法Sedgewick第四版-第1章基础-017一约瑟夫问题(Josephus Problem)

2016-04-19 18:01 162 查看
/*************************************************************************
*
*  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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: