Java解决约瑟夫问题代码实例
发布时间:2023-09-20 19:17:02
import java.util.ArrayList;
/**
* Java约瑟夫问题: n个人(不同id)围成一个圈,从startId(任意数)个开始报数m(任意数)个数,数m的人出列排成新队列,m清零,
* 然后又从下一个人开始数m个数开始,数到m就出列接在新队列尾部,如此重复,知道所有人都出列为止。
* 打印 出列后的新队列
*
* eg
* int n = 10;//总人数
* int m = 3; //报数个数
* int startIndex = 1; //起点位置
* @author Hulk 2014 03 20
*
*/
public class JosephListTest {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
JosephListTest test = new JosephListTest();
int n = 10; //总人数
int m = 3; //报数个数
int startIndex = 12; //起点位置
System.out.println("JosephListTest: n= " + n + ", m= " + m +
", startIndex= " + startIndex + "\n\nQUEUE RESULT:");
ArrayList<Person> queueList = test.queuePreson(n, m, startIndex);
for (Person person : queueList) {
System.out.println("OUT person: " + person);
}
System.out.println("use time= " +
(System.currentTimeMillis() - startTime));
}
private ArrayList<Person> queuePreson(int n, int m, int startIndex) {
ArrayList<Person> queueList = null;
PersonList list = createList(n);
//list.search();
if ((list != null) && (list.head != null)) {
queueList = new ArrayList<JosephListTest.Person>();
PNode pNode = list.head;
if (startIndex > 0) {
startIndex = startIndex % n;
pNode = list.getNode(startIndex);
} else {
pNode = list.head;
}
int count = 1;
while (list.size > 0) {
Person outPerson = null;
//find
if (count == (m - 1)) {
//delete next node
Person prev = pNode.person;
outPerson = list.remove(prev);
queueList.add(outPerson);
//System.out.println("OUT person: " + outPerson + ", size= " + list.size);
count = 0;
}
pNode = pNode.next;
count++;
}
}
return queueList;
}
private PersonList createList(int n) {
PersonList list = new PersonList();
for (int i = 0; i < n; i++) {
Person person = new Person(i, "name_" + (i + 1));
list.add(i, person);
}
return list;
}
public class PersonList {
PNode head = null;
int size = 0;
public PersonList() {
}
public PersonList(Person person) {
head = new PNode(person, head);
size++;
}
public PersonList(PNode head) {
this.head = head;
head.setNext(head);
size++;
}
public PNode getHead() {
return head;
}
public void setHead(PNode head) {
this.head = head;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public void size(int size) {
this.size = size;
}
public boolean isEmpty() {
return this.size <= 0;
}
public void initHead(Person person) {
if (size == 0) {
head = new PNode(person, head);
} else {
PNode no = head;
head = new PNode(person, no);
}
size++;
}
public void add(int index, Person person) {
if (size == 0) {
head = new PNode(person, head);
head.setNext(head);
//System.out.println("head: " + head);
} else {
if (index < 0) {
index = 0;
}
if (index > size) {
index = size;
}
PNode no = head;
for (int i = 0; i < (index - 1); i++) {
no = no.next;
}
PNode newNode = new PNode(person, no.next);
no.next = newNode;
}
size++;
}
public Person delete(int index) {
PNode pNode = remove(index);
if ((pNode != null) && (pNode.next != null)) {
return pNode.next.person;
}
return null;
}
public PNode remove(int index) {
if (size == 0) {
return null;
} else {
if ((index < 0) || (index >= size)) {
return null;
}
}
PNode no = head;
for (int i = 0; i < (index - 1); i++) {
no = no.next;
}
no.next = no.next.next;
size--;
if ((no != null) && (no.next != null)) {
return no.next;
}
return null;
}
/**
* remove next node of person node, and return the deleted person
* @param prePerson
* @return removed Person
*/
public Person remove(Person prePerson) {
if (prePerson == null) {
return null;
}
if (size == 0) {
return null;
}
PNode preNode = head;
int index = -1;
for (int i = 0; i < size; i++) {
if (preNode.person.id == prePerson.id) {
index = i;
break;
} else {
preNode = preNode.next;
}
}
Person remPerson = null;
if (size <= 1) {
//only one node, get its person and set it as null
remPerson = preNode.person;
preNode = null;
} else {
//preNode.next.person is dest one
remPerson = preNode.next.person;
preNode.next = preNode.next.next;
}
size--;
//System.out.println("deleteing index= " + index + " : " + remPerson + ", size= " + size);
return remPerson;
}
public int update(Person src, Person dest) {
if (src == null) {
return -1;
}
int index = -1;
PNode no = head;
for (int i = 0; i < size; i++) {
if (src.id == no.person.id) {
no.person = dest;
break;
} else {
no = no.next;
}
}
return index;
}
public boolean set(int index, Person person) {
if (person == null) {
return false;
}
if (size == 0) {
return false;
} else {
if ((index < 0) || (index >= size)) {
return false;
}
}
PNode no = head;
for (int i = 0; i < index; i++) {
no = no.next;
}
no.person = person;
return true;
}
public Person get(int index) {
PNode no = getNode(index);
if (no != null) {
return no.person;
}
return null;
}
public PNode getNode(int index) {
if (size == 0) {
return null;
} else {
if ((index < 0) || (index >= size)) {
return null;
}
}
PNode no = head;
for (int i = 0; i < index; i++) {
no = no.next;
}
return no;
}
public void search() {
int sizeLong = size;
PNode no = head;
for (int i = 0; i < sizeLong; i++) {
System.out.println("search index= " + i + ", " + no);
no = no.next;
}
}
}
public class PNode {
Person person;
PNode next = null;
public PNode() {
}
public PNode(Person person) {
this.person = person;
}
public PNode(Person person, PNode next) {
this.person = person;
this.next = next;
}
@Override
public String toString() {
return "PNode [person=" + person.id + ", next=" + next.person.id +
"]";
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public PNode getNext() {
return next;
}
public void setNext(PNode next) {
this.next = next;
}
}
public class Person {
int id = 0;
String name = "";
public Person() {
}
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
}


猜你喜欢
- 前端页面功能模块化拆分当一个系统的功能很多时,不可能所有功能模块的页面都写在一个页面里面,这时就需要将不同功能模块的页面拆分出去,就像模板一
- 线程组线程组可以批量管理线程和线程组对象。一级关联例子如下,建立一级关联。public class MyThread43 implement
- 首先来看看以下程序将会打印出什么:class Dog { public static void bark
- 一,项目简介项目编号:BS-SC-029本系统主要为种值水果和农户和水果经销商搭建一个B2B的电子商务平台,系统共包含三个角色:农户、经销商
- 项目需要去调用.NET的WebSrevice,本身是Java,研究了半天,终于有些头绪,记下来。1,新建.NET WebService。只在
- 首先,查到java里文件重命名的方法为:renameTo();我将180张图片放在d:\\backup下,用下面的程序进行重命名:publi
- 这几天自己研究了关于地手机上面开发安卓地图的问题,发现百度官方示例demo讲解百度持续定位方面还是讲解的有些不清楚,本人研究了几次之后将其弄
- 使用Spring3 实现用户登录以及权限认证这里我就简单介绍一下,我在实现的时候处理的一些主要的实现。1.用户登录 <form act
- 前言前段时间一直使用到word文档转pdf或者pdf转word,寻思着用Java应该是可以实现的,于是花了点时间写了个文件转换工具源码wel
- 前言继上次skywalking出故障《解析Arthas协助排查线上skywalking不可用问题》不到一个月,线上skywalking又出毛
- 1.构造器也就是在上一篇讲的那个例子,调用默认的无参构造函数2.静态工厂方法1)创建需要执行的方法的类public class HelloW
- 这篇文章主要介绍了Java如何实现支付宝电脑支付基于servlet版本,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学
- 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应, 同时我们又需要在工作线程中更新UI界面上的控件。但直接访
- 在这篇文章中,我们将介绍如下内容:==运算符与基元类型==运算符与引用类型==运算符与String类型==运算符与值类型==运算符与泛型==
- 前言ParametersInterceptor * 其主要功能是把ActionContext中的请求参数设置到ValueStack中,如果栈
- JSONObject的使用 一、 JSON对象的使用:String content = "{'username&
- 前言最近公司有了新的业务,把现有Flutter Android项目应用到TV上去,这不,Asscre的活就来了。本文详细说明Flutter
- springboot启动失败的问题springboot版本是1.3.0.M1,连接的mysql版本为8,用spring-boot-start
- 目录1、一个抽象类并不需要其中所有的方法都是抽象的。( )2、下列程序的运行结果3、在Java中,关于HashMap类的描述,以下错误的是(
- 本文实例为大家分享了Unity3D Shader实现扫描显示的具体代码,供大家参考,具体内容如下通过Shader实现,从左向右的扫描显示,可