您的位置:首页 > 编程语言 > Go语言

算法(Algorithms)第4版 练习 1.3.37

2017-03-08 16:48 429 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: