java代码约瑟夫出圈 java 约瑟夫-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

java代码约瑟夫出圈 java 约瑟夫

java约瑟夫出圈问题

public class YueSeFuCircle

十余年的阿鲁科尔沁网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整阿鲁科尔沁建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“阿鲁科尔沁网站设计”,“阿鲁科尔沁网站推广”以来,每个客户项目都认真落实执行。

{

public static void main(String[] args)

{

int n = 100;//总共多少人

int kill = 3;//要出圈的数字

int winner = 0;

for (int i = 2; i = n; i++)

{

winner = (winner + kill) % i;

System.out.println(winner);

}

System.out.println("Winner is:" + (winner + 1));

}

}

关于约瑟夫环问题,用java 编写程序,输出n个人出圈的顺序,书上的程序代码如下,但是有几点我搞不明白

你这个好麻烦啊, 理解 队列 和 桟 的区别

队伍    先进先出  FIFO

桟       先进后出  LIFO

本程序明显是队伍!!!

import java.util.* ;

public class T {

public static void main(String[] args) {

ListInteger list = new LinkedListInteger();  

//模拟15个人,编号是1-15  

for(int i=1 ; i=15 ; i++) {

list.add(i);

}  

//模拟 叫到7的人出圈  

show(list , 7);

public static void show(ListInteger list  , int n){

int i = 1 ;//每次报的数字 

int count = 1 ;//记录第几次出圈 

while(true){

//当list长度为1的时候代表就剩下1个人 

if(list.size()==1){

System.out.println("第"+count+++"次是"+list.get(0));

return ;

}

//每次获取队里第一个人 

int value = list.get(0);

//报完数后移开该人 

list.remove(0);

//如果报数的人报的数字为n ,则打印 

if(i == n) {

i = 1 ;

System.out.println("第"+count+++"次是"+value);

}else {

//如果报数的人报的数字不是n ,将该人排到队伍的后面,以便下次继续叫号 

list.add(value);

//产生下次报数的数字 

i++ ;

}

}

}

}

用java解决约瑟夫问题

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:");

ArrayListPerson 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 ArrayListPerson queuePreson(int n, int m, int startIndex) {

ArrayListPerson queueList = null;

PersonList list = createList(n);

//list.search();

if ((list != null) (list.head != null)) {

queueList = new ArrayListJosephListTest.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 + "]";

}

}

}

怎么用java数组实现约瑟夫环

用java数组实现约瑟夫环

package Josephround;

public class Joseround {

int sit;

int flagjo=0;

Joseround(){};

Joseround(int x){

sit=x;

}

void setflag(int x){

flagjo=x;

}

}

package Josephround;

public class Inijose {

Joseround jo[];

static int length=0;

Inijose(){};

Inijose(int x){

jo=new Joseround[x];

for(int i=0;ix;i++){

jo[i]=new Joseround(i+1);//创建对象数组

length++;

}

}

void delete(int n){

for(int i=n;ilength-1;i++){

jo[i]=jo[i+1];

}

length--;

}

}

package Josephround;

import java.util.Scanner;

public class Text {

public static void main(String[] args) {

int m,n;

System.out.println("input m");

Scanner m1=new Scanner(System.in);

m=m1.nextInt();

System.out.println("input n");

Scanner n1=new Scanner(System.in);

n=n1.nextInt();

int temp=0;

int x=0;

Inijose joseph=new Inijose(n);

while(joseph.length!=0){

for(int i=1;i=m;i++){

joseph.jo[x].setflag(i);

if(joseph.jo[x].flagjo==m){

System.out.println(joseph.jo[x].sit);

joseph.delete(x);

x--;

}

if(xjoseph.length-1) x++;

else x=0;

}

}

}

}


网页标题:java代码约瑟夫出圈 java 约瑟夫
文章分享:http://kswsj.cn/article/dddjcoo.html

其他资讯