【DataStructure】 Classical Question: Josephus Cycle

2024-03-02 20:32

本文主要是介绍【DataStructure】 Classical Question: Josephus Cycle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【Description】

  This problem is based upon a report by the historian Joseph ben Matthias (Josephus) on the outcome of a suicide pact that he had made between himself and 40 soldiers as they were besieged by superior Roman forces in 67 A.D. Josephus proposed that each man slay his neighbor. This scheme necessarily leaves one to kill himself. Josephus cleverly contrived to be that one, thus surviving to tell the tale.

【Implementation】

package com.albertshao.ds.ring;//  Data Structures with Java, Second Edition
//  by John R. Hubbard
//  Copyright 2007 by McGraw-Hillimport java.util.*;public class Ring<E> extends AbstractList<E> implements List<E> {private Node<E> last;private int size;public boolean add(E element) {if (size == 0) {last = new Node<E>(element, null);last.next = last;} else {last.next = new Node<E>(element, last.next);last = last.next;}++size;return true;}public E get(int index) {if (index < 0 || index >= size) {throw new IndexOutOfBoundsException();}Node<E> p = last.next;for (int i=0; i<index; i++) {p = p.next;}return p.element;}public Iterator<E> iterator() {return new RingIterator();}public String toString() {Node<E> p = last.next;StringBuilder buf = new StringBuilder("[" + p.element);while (p != last) {p = p.next;buf.append(", " + p.element);}buf.append("]");return buf.toString();}public int size() {return size;}private class RingIterator implements Iterator<E> {private Node<E> preLastNext = last;private Node<E> lastNext;public boolean hasNext() {return size > 0;}public E next() {if (lastNext == null) {lastNext = preLastNext.next;} else {preLastNext = lastNext;lastNext = lastNext.next;}return lastNext.element;}public void remove() {if (lastNext == null) {throw new IllegalStateException();}if (size == 1) {last = preLastNext = null;} else {preLastNext.next = lastNext.next;}if (lastNext == last) {last = preLastNext;}lastNext = null;--size;}}private static class Node<E> {E element;Node<E> next;Node(E element, Node<E> next) {this.element = element;this.next = next;}}
}

package com.albertshao.ds.ring;//  Data Structures with Java, Second Edition
//  by John R. Hubbard
//  Copyright 2007 by McGraw-Hillimport java.util.*;public class Josephus {public static final int SOLDIERS = 8;public static final String ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";public static void main(String[] args) {Ring<String> ring = new Ring<String>();for (int i=0; i<SOLDIERS; i++) {ring.add(ALPHA.substring(i, i+1));}System.out.println(ring);Iterator<String> it = ring.iterator();String killer = it.next();while (ring.size() > 1) {String victim = it.next();System.out.println(killer + " killed " + victim);it.remove();killer = it.next();}System.out.println("The lone survivor is " + it.next());}
}

【Result】

[A, B, C, D, E, F, G, H]
A killed B
C killed D
E killed F
G killed H
A killed C
E killed G
A killed E
The lone survivor is A


这篇关于【DataStructure】 Classical Question: Josephus Cycle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/767334

相关文章

Josephus 问题

有 𝑛 个人围成一个圈,依次标号 0 至 𝑛 − 1。从 0 号开始,依次 0,1,0,1,… 交替报数,报到 1 的人会离开,直至圈中只剩下一个人。求最后剩下人的编号。   #include <iostream>using namespace std;bool b[1000010]={0};int main(){//模拟程序int n;cin>>n;int cnt=0;//淘汰数i

代理模式正常启动项目,访问项目提示:Cycle Detected

现象:本地配置了代理模式上网,浏览器也同样在设置里配置了代理模式,就这样启动项目之后,控制台没有错误日志启动成功,但是访问项目的时候,页面显示:Cycle Detected 下面写着英文汉语大意是: 我的请求是一个死循环。 原因:项目没有问题,可能是代理配置哪里有问题 解决办法:浏览器 设置 高级设置,代理模式,局域网设置,本地访问不采用代理模式 这个要打上勾,即可!刷新访问路径就

Leetcode Question 高频 和 分类

Leetcode Question Difficulty and Frequency 题目分类: Dynamic Programming Edit DistanceMaximum SubarrayMinimum Path SumUnique PathsUnique Paths IILongest Palindromic SubstringInterleaving StringT

Classical Binary Search

Find any position of a target number in a sorted array. Return -1 if target does not exist. Example Example 1: Input: nums = [1,2,2,4,5,5], target = 2Output: 1 or 2 Example 2: Input: nums = [1,

Question mutiple pdf‘s using openai, pinecone, langchain

题意:使用 OpenAI、Pinecone 和 LangChain 对多个 PDF 文件进行提问。 问题背景: I am trying to ask questions against a multiple pdf using pinecone and openAI but I dont know how to. 我正在尝试使用 Pinecone 和 OpenAI 对多个 PDF 文

Answer's Question about pointer

When you create a new pointer, this will be in heap until you delete it.  So what you said is sort of mistake, "函数内部有个局部变量指针", then the pointer should not exist after the function return. Ther

Leetcode78: Linked List Cycle II

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up: Can you solve it without using extra space? 大

【BigHereo 44】---DataStructure---队列(二)

Datastructure---队列   一,【前言】        前面我们说到,数据结构中,有线性和非线性的,今天我们主要来总结一下线性结构---队列. 了解队列,我们先从几个简单问题入手:    (1)顺序表查的效率高吗?    (2)循环队列中,实际队列长的怎么算?    (3)常用循环语句有哪些?    (4)数据项和数据元素有什么

【Python】Itertools.cycle()用法及代码示例

让迭代器可以无限循环迭代。 迭代器定义为对象类型,其中包含可以使用循环访问或迭代的值。 内置与Python一起提供了不同的迭代器,例如列表,集合等。Itertools是Python模块,其中包含一些用于使用迭代器生成序列的内置函数。该模块提供了在迭代器上工作以生成复杂迭代器的各种功能。该模块可作为一种快速的内存有效工具,可单独使用或组合使用以形成迭代器代数。 有不同类型的迭代器 无限迭

Automatic Educational Question Generation with Difficulty Level Controls

文章目录 题目摘要简介相关工作问题表述实验用户研究结论 题目 具有难度级别控制的自动教育问题生成 论文地址:https://link.springer.com/chapter/10.1007/978-3-031-36272-9_39 摘要     我们考虑自动生成各种难度的数学应用题 (MWP),以满足教师在相应教育阶段教学和测试学生的需求。现有方法无法生成高质