算法Sedgewick第四版-第1章基础-017一约瑟夫问题(Josephus Problem)
/*************************************************************************
*
* 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)的更多相关文章
- 算法Sedgewick第四版-第1章基础-001递归
一. 方法可以调用自己(如果你对递归概念感到奇怪,请完成练习 1.1.16 到练习 1.1.22).例如,下面给出了 BinarySearch 的 rank() 方法的另一种实现.我们会经常使用递归, ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-001选择排序法(Selection sort)
一.介绍 1.算法的时间和空间间复杂度 2.特点 Running time is insensitive to input. The process of finding the smallest i ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- 算法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 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-005插入排序的改进版
package algorithms.elementary21; import algorithms.util.StdIn; import algorithms.util.StdOut; /***** ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-004希尔排序法(Shell Sort)
一.介绍 1.希尔排序的思路:希尔排序是插入排序的改进.当输入的数据,顺序是很乱时,插入排序会产生大量的交换元素的操作,比如array[n]的最小的元素在最后,则要经过n-1次交换才能排到第一位,因为 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-002插入排序法(Insertion sort)
一.介绍 1.时间和空间复杂度 运行过程 2.特点: (1)对于已排序或接近排好的数据,速度很快 (2)对于部分排好序的输入,速度快 二.代码 package algorithms.elementar ...
- 算法Sedgewick第四版-第1章基础-1.3Bags, Queues, and Stacks-001可变在小的
1. package algorithms.stacks13; /******************************************************************* ...
- 算法Sedgewick第四版-第1章基础-1.4 Analysis of Algorithms-005计测试算法
1. package algorithms.analysis14; import algorithms.util.StdOut; import algorithms.util.StdRandom; / ...
随机推荐
- js操作获取和设置cookie
//创建cookie function setCookie(name, value, expires, path, domain, secure) { var cookieText = encodeU ...
- JS开发页面小组件:table组件
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- yeomen/bower/grunt
yeomen: npm install yo angular-in-action project npm install -g generator-angular npm install -g gen ...
- Nhibernate系列学习之(五) 存储过程
NHibernate也是能够操作存储过程的,不过第一次配置可能会碰到很多错误. 一.删除 首先,我们新建一个存储过程如下: CREATE PROC DeletePerson @Id int AS DE ...
- New Concept English three (54)
打字练习: 27w/m 45errors We have been brought up to fear insects. We regard them as unnecessary creature ...
- hdu 4445 Crazy Tank(物理过程枚举)
遇到物理题,千万不要一味的当成物理题去想着推出一个最终结果来,这样ACM竞赛成了物理比赛,出题人就没水平了...往往只需要基础的物理分析,然后还是用算法去解决问题.这题n小于等于200,一看就估计是暴 ...
- 在winform下实现左右布局多窗口界面的方法(一)
在web页面上我们可以通过frameset,iframe嵌套框架很容易实现各种导航+内容的布局界面,而在winform.WPF中实现其实也很容易,通过本文给大家介绍在winform下实现左右布局多窗口 ...
- win8.1系统相关
win8.1系统相关 信息时代,系统更新速度非常快,十一月初,同事在网上花5元买了一个win8.1系统激活码,之后两周,我电脑由于系统故障,准备重装系统,借助他的系统,但无法激活,借用他购买的账号也不 ...
- mysqldump全备份脚本mysqlallbackup.sh
库小,大概16G左右,每天增量很小,不到100M,所以用mysqldump每天全量备份,将备份结果信息发送到email通知DBA. mysqlallbackup.sh :MySQL DataBase ...
- Azure上采用Json Template从已有的VHD创建VM
从已有的VHD创建VM是使用Azure中经常要操作的内容. 本文将介绍如何采用Json Template从已经有的VHD创建VM. 一.准备VHD 在我的Azure账户中选择一台VM,如下图: 查看其 ...