真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java代碼約瑟夫出圈 java 約瑟夫

java約瑟夫出圈問(wèn)題

public class YueSeFuCircle

十余年的阿魯科爾沁網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。成都全網(wǎng)營(yíng)銷(xiāo)推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整阿魯科爾沁建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“阿魯科爾沁網(wǎng)站設(shè)計(jì)”,“阿魯科爾沁網(wǎng)站推廣”以來(lái),每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

{

public static void main(String[] args)

{

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

int kill = 3;//要出圈的數(shù)字

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

}

}

關(guān)于約瑟夫環(huán)問(wèn)題,用java 編寫(xiě)程序,輸出n個(gè)人出圈的順序,書(shū)上的程序代碼如下,但是有幾點(diǎn)我搞不明白

你這個(gè)好麻煩啊, 理解 隊(duì)列 和 桟 的區(qū)別

隊(duì)伍??? 先進(jìn)先出??FIFO

桟?????? 先進(jìn)后出? LIFO

本程序明顯是隊(duì)伍?。。?/p>

import?java.util.*?;

public?class?T?{

public?static?void?main(String[]?args)?{

ListInteger?list?=?new?LinkedListInteger();??

//模擬15個(gè)人,編號(hào)是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?;//每次報(bào)的數(shù)字?

int?count?=?1?;//記錄第幾次出圈?

while(true){

//當(dāng)list長(zhǎng)度為1的時(shí)候代表就剩下1個(gè)人?

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

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

return?;

}

//每次獲取隊(duì)里第一個(gè)人?

int?value?=?list.get(0);

//報(bào)完數(shù)后移開(kāi)該人?

list.remove(0);

//如果報(bào)數(shù)的人報(bào)的數(shù)字為n?,則打印?

if(i?==?n)?{

i?=?1?;

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

}else?{

//如果報(bào)數(shù)的人報(bào)的數(shù)字不是n?,將該人排到隊(duì)伍的后面,以便下次繼續(xù)叫號(hào)?

list.add(value);

//產(chǎn)生下次報(bào)數(shù)的數(shù)字?

i++?;

}

}

}

}

用java解決約瑟夫問(wèn)題

Java約瑟夫問(wèn)題: n個(gè)人(不同id)圍成一個(gè)圈,從startId(任意數(shù))個(gè)開(kāi)始報(bào)數(shù)m(任意數(shù))個(gè)數(shù),數(shù)m的人出列排成新隊(duì)列,m清零,然后又從下一個(gè)人開(kāi)始數(shù)m個(gè)數(shù)開(kāi)始,數(shù)到m就出列接在新隊(duì)列尾部,如此重復(fù),知道所有人都出列為止。

package list;

import java.util.ArrayList;

* 打印 出列后的新隊(duì)列

*

* eg

* int n = 10;//總?cè)藬?shù)

* int m = 3; //報(bào)數(shù)個(gè)數(shù)

* int startIndex = 1; //起點(diǎn)位置

* @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; //總?cè)藬?shù)

int m = 3; //報(bào)數(shù)個(gè)數(shù)

int startIndex = 12; //起點(diǎn)位置

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數(shù)組實(shí)現(xiàn)約瑟夫環(huán)

用java數(shù)組實(shí)現(xiàn)約瑟夫環(huán)

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);//創(chuàng)建對(duì)象數(shù)組

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;

}

}

}

}


本文標(biāo)題:java代碼約瑟夫出圈 java 約瑟夫
分享鏈接:http://weahome.cn/article/dddjcoo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部