用java解决约瑟夫问题
帮忙看下这个方法解决约瑟夫问题有什么问题,有些数据能正确处理,有些不能,给为帮忙改下,谢谢packagetest;importjava.util.*;publicclas...
帮忙看下这个方法解决 约瑟夫问题有什么问题,有些数据能正确处理,有些不能,给为帮忙改下,谢谢
package test;
import java.util.*;
public class CircleTable{
private int peopleNumber; //人数
private int lengthInterval; //间隔长度
private LinkedList<Integer> list1;
CircleTable(int peoplenumber,int lengthinterval)
{
peopleNumber = peoplenumber;
lengthInterval= lengthinterval;
list1 = new LinkedList<Integer>();
}
public void setArray()
{
for (int i = 1; i <= peopleNumber; i++)
list1.add(i);
}
public void outOfGroup()
{
int i = 0,count = 1;
int tmp = 0;
while (peopleNumber > 1)
{
for (int j = 0; j < (lengthInterval - 1); j++)
{
if (list1.size()==2) {i = 0;}
if ((tmp = list1.get(i)) == list1.getLast())
{i = 0;}
else
{i++;}
}
System.out.print("第" + count + "个出局的人:");
System.out.println(list1.get(i));
list1.remove(i);
peopleNumber--;
count++;
} //while (peopleNumber > 1)
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("请输入人数:");
int peopleNumber = s.nextInt();
System.out.print("请输入不长:");
int lengthInterval = s.nextInt();
s.close();
CircleTable circleTable = new CircleTable(peopleNumber,lengthInterval);
circleTable.setArray();
circleTable.outOfGroup();
}
}
我的问题是n个人,从一数到m,数到m的人出圈,从下一个开始再从一开始数到m,再出圈,.......,直到只剩一个人为止 展开
package test;
import java.util.*;
public class CircleTable{
private int peopleNumber; //人数
private int lengthInterval; //间隔长度
private LinkedList<Integer> list1;
CircleTable(int peoplenumber,int lengthinterval)
{
peopleNumber = peoplenumber;
lengthInterval= lengthinterval;
list1 = new LinkedList<Integer>();
}
public void setArray()
{
for (int i = 1; i <= peopleNumber; i++)
list1.add(i);
}
public void outOfGroup()
{
int i = 0,count = 1;
int tmp = 0;
while (peopleNumber > 1)
{
for (int j = 0; j < (lengthInterval - 1); j++)
{
if (list1.size()==2) {i = 0;}
if ((tmp = list1.get(i)) == list1.getLast())
{i = 0;}
else
{i++;}
}
System.out.print("第" + count + "个出局的人:");
System.out.println(list1.get(i));
list1.remove(i);
peopleNumber--;
count++;
} //while (peopleNumber > 1)
}
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.print("请输入人数:");
int peopleNumber = s.nextInt();
System.out.print("请输入不长:");
int lengthInterval = s.nextInt();
s.close();
CircleTable circleTable = new CircleTable(peopleNumber,lengthInterval);
circleTable.setArray();
circleTable.outOfGroup();
}
}
我的问题是n个人,从一数到m,数到m的人出圈,从下一个开始再从一开始数到m,再出圈,.......,直到只剩一个人为止 展开
推荐于2016-08-20 · 知道合伙人互联网行家
关注
展开全部
Java约瑟夫问题: n个人(不同id)围成一个圈,从startId(任意数)个开始报数m(任意数)个数,数m的人出列排成新队列,m清零,然后又从下一个人开始数m个数开始,数到m就出列接在新队列尾部,如此重复,知道所有人都出列为止。
package list;
import java.util.ArrayList;
* 打印 出列后的新队列
*
* 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 + "]";
}
}
}
package list;
import java.util.ArrayList;
* 打印 出列后的新队列
*
* 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 + "]";
}
}
}
展开全部
约瑟夫问题
这是17世纪的法国数学家加斯帕在《数目的游戏问题》中讲的一个故事:15个教徒和15 个非教徒在深海上遇险,必须将一半的人投入海中,其余的人才能幸免于难,于是想了一个办法:30个人围成一圆圈,从第一个人开始依次报数,每数到第九个人就将他扔入大海,如此循环进行直到仅余15个人为止。问怎样排法,才能使每次投入大海的都是非教徒。
这个问题和下面的故事差不多:
有一群人被困在了一个小岛上,他们只有一个竹排可以逃生,竹排只能坐一个人,于是他们决写通过玩退圈游戏来决定哪个人可以坐上竹排逃生。这些人围成了一个圆圈,从1开始每个人顺序编号,他们商定了一个不幸的数字X,然后,从编号为1的人开始报数,报到X的倍数就退出游戏,直到最后剩下一个人,这个人就是可以得到竹排的人编写程序。在Main方法中输入玩游戏的人数和不幸的数字X,自定义Play方法按游戏规则进行游戏,方法返回赢的人编号.
有多少人参加游戏?3
数到哪个数字就退出?7
编号为3的人得到了竹排
你可以参考一下它里面的算法。参考资料中描述得很详细。你可以看看的。
还有运行的结果。你的代码我没有看明白,可能是算法错了。
这是17世纪的法国数学家加斯帕在《数目的游戏问题》中讲的一个故事:15个教徒和15 个非教徒在深海上遇险,必须将一半的人投入海中,其余的人才能幸免于难,于是想了一个办法:30个人围成一圆圈,从第一个人开始依次报数,每数到第九个人就将他扔入大海,如此循环进行直到仅余15个人为止。问怎样排法,才能使每次投入大海的都是非教徒。
这个问题和下面的故事差不多:
有一群人被困在了一个小岛上,他们只有一个竹排可以逃生,竹排只能坐一个人,于是他们决写通过玩退圈游戏来决定哪个人可以坐上竹排逃生。这些人围成了一个圆圈,从1开始每个人顺序编号,他们商定了一个不幸的数字X,然后,从编号为1的人开始报数,报到X的倍数就退出游戏,直到最后剩下一个人,这个人就是可以得到竹排的人编写程序。在Main方法中输入玩游戏的人数和不幸的数字X,自定义Play方法按游戏规则进行游戏,方法返回赢的人编号.
有多少人参加游戏?3
数到哪个数字就退出?7
编号为3的人得到了竹排
你可以参考一下它里面的算法。参考资料中描述得很详细。你可以看看的。
还有运行的结果。你的代码我没有看明白,可能是算法错了。
参考资料: http://hi.baidu.com/zpz2009/blog/item/f9b52e18c9c8e34d43a9ad93.html
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询