/*************************************************************************
*
* 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();
}
}

算法Sedgewick第四版-第1章基础-017一约瑟夫问题(Josephus Problem)的更多相关文章

  1. 算法Sedgewick第四版-第1章基础-001递归

    一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...

  2. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)

    一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...

  3. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)

    一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...

  4. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-006归并排序(Mergesort)

    一. 1.特点 (1)merge-sort : to sort an array, divide it into two halves, sort the two halves (recursivel ...

  5. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版

    package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...

  6. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)

    一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...

  7. 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)

    一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...

  8. 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的

    1. package algorithms.stacks13; /******************************************************************* ...

  9. 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法

    1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...

随机推荐

  1. weinre

    https://www.cnblogs.com/diva/p/3995674.html

  2. CodeForces - 900D: Unusual Sequences (容斥&莫比乌斯&组合数学)

    Count the number of distinct sequences a1, a2, ..., an (1 ≤ ai) consisting of positive integers such ...

  3. NET Core 2.0利用MassTransit集成RabbitMQ

    NET Core 2.0利用MassTransit集成RabbitMQ https://www.cnblogs.com/Andre/p/9579764.html 在ASP.NET Core上利用Mas ...

  4. Rabbitmq用户权限配置

    由于账号guest具有所有的操作权限,并且又是默认账号,出于安全因素的考虑,guest用户只能通过localhost登陆使用,并建议修改guest用户的密码以及新建其他账号管理使用rabbitmq(该 ...

  5. SWT与Linux安装包

    关于SWT SWT首先要在Eclipse中添加SWT的安装包:Windowsbuilder Pro.下载路径:http://www.eclipse.org/windowbuilder/download ...

  6. Dockerfile 部署应用执行脚本文件

    FROM centos6.6:0.0.1 MAINTAINER syberos:wangmo RUN mv /etc/yum.repos.d/ /etc/yum.repos.d_bak/ && ...

  7. Oracle session出现大量的inactive

    一.官网说明 1.1 processes 11gR2 的文档: Property Description Parameter type Integer Default value 100 Modifi ...

  8. 手机的RAM和ROM

    RAM是由英文Random Access Memory的首字母构成的,意为随机存储器,即在正常工作状态下可以往存储器中随时读写数据.根据存储单元工作原理的不同,RAM又可分为静态存储器(SRAM)和动 ...

  9. Drools学习笔记2—Conditions / LHS 匹配模式&条件元素

    Rule的LHS由条件元素(Conditional Elements—CE)和匹配模式(Patterns)组成 Patterns被用来指示出fact的字段约束 每个约束必须为true才能让RHS的ac ...

  10. Eloquent ORM模型中添加自定义值

    我们都知道通过Laravel中数据库查询出来的模型对象都是基于数据库字段,今天给大家展示一个 Laravel Eloquent ORM 模型特性-附加值不存在于数据表中. 举个简单的栗子,一篇文章(p ...