約瑟夫環java數組

本文目錄一覽:

高分求解java約瑟夫環問題

package josephus

/**

* pTitle: Josephus/p

* pDescription: This is a algorithm to display Josephus/p

* pCopyright: Copyright (c) 2007/p

* pCompany: BHL/p

* @author Linpeizhong

* @version 1.0

*/

import javax.swing.JLabel;

import javax.swing.border.Border;

import java.awt.Color;

import javax.swing.BorderFactory;

import java.awt.SystemColor;

/**節點類,用於每個節點屬性的定義*/

class Node {

int flag; //序號

Node next; //指針

int mima; //密碼

JLabel jLabel; //擁有的標籤

//構造器方法,用於實現每個節點的密碼序號m,密碼n,標籤JLabel

public Node(int m, int n, JLabel jLabel) {

flag = m;

mima = n;

this.jLabel = jLabel;

}

}

/**主功能類,用於運行時的界面控制*/

public class MainSrc {

//節點數

int s = 12;

//初始密碼

int cs = 0;

//定義節點數組

int a[] = new int[s];

//聲明界面類MainApplet的對象

MainApplet mainApplet;

//邊線對象

Border border, border2;

//主功能類的構造器方法,用於對象數組,初始密碼,界面類的初始化

public MainSrc(int a[], int cs, MainApplet mainApplet) {

this.a = a;

this.cs = cs;

this.mainApplet = mainApplet;

//調用初始化方法

init();

}

//初始化方法

public void init() {

//當點擊主界面的按鈕“開始”時,將調用MainSrc這個功能類,調用後“開始”按鈕應該設置為不可用

mainApplet.jButton2.setEnabled(false);

//紅色邊線

border = BorderFactory.createLineBorder(Color.red, 10);

//系統控制的顏色的邊線,也就是黑色邊線

border2 = BorderFactory.createLineBorder(SystemColor.controlText, 1);

//賦值

int m1 = a[0], m2 = 0, m3 = cs;

//節點1的實現

Node a1 = new Node(1, m1, mainApplet.jLabels[0]);

//當前節點定位為節點a1

Node index = a1;

//初始化a1後的各個節點

for (int i = 2; i = s; i++) { //初始化每個人

//通過密碼數組的元素賦值密碼

m2 = a[i – 1];

//讓當前的節點的指針域指向下個節點

index.next = new Node(i, m2, mainApplet.jLabels[i – 1]);

//讓當前節點的下個節點成為當前節點

index = index.next;

}

/**通過上面的賦值後,index最後定位在最後一個節點上,也就是第12個節點上,讓

* 最後一個節點的指針域指向節點a1,這樣就構造出了一個帶有節點Node的循環單鏈表

*/

index.next = a1;

//當i=s時,循環結束,出隊完畢

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

for (int j = 0; j m3 – 1; j++) {

index = index.next;

//讓所有人的邊線為黑色

for (int z = 0; z mainApplet.jLabels.length; z++) {

mainApplet.jLabels[z].setBorder(border2);

}

//輪到報數的人的邊線為紅色

index.jLabel.setBorder(border);

//報數時時間暫停為界面類傳遞過來sd變量的值,而sd是通過JSlider來設置的,因而JSlider可以調節報數速度

try {

Thread.sleep(mainApplet.sd);

}

catch (InterruptedException ex1) {

ex1.printStackTrace();

}

}

//設置出列的前一位的邊線為原先的顏色

index.jLabel.setBorder(border2);

//設置該出列的人的邊線為

index.next.jLabel.setBorder(border);

//讓紅色邊線在將要出列的人的地方停留時間tostop,同樣也是通過組件JSlider調節

try {

Thread.sleep(mainApplet.toStop);

}

catch (InterruptedException ex2) {

ex2.printStackTrace();

}

//測試時使用,用於在控制台顯示將要出列的人的號數

System.out.println("第" + index.next.flag + "人出局");

//在界面類的狀態窗口顯示出列情況

mainApplet.jTextArea1.append(index.next.flag + " ");

//讓出列的號數的標籤為不可見

index.next.jLabel.setVisible(false);

try {

Thread.sleep(1000);

}

catch (InterruptedException ex) {

}

//把出列的號所擁有的密碼為運行的密碼

m3 = index.next.mima;

//刪除節點,其實在鏈表中是讓指針指向改變而已,這是出列,也是算法的重點

index.next = index.next.next;

}

//讓所有的標籤的邊線為原先的顏色

for (int z = 0; z mainApplet.jLabels.length; z++) {

mainApplet.jLabels[z].setBorder(border2);

}

//

mainApplet.jTextArea1.append("\n哈哈,測試結束結束啦!\n請觀察測試的數據是不是和你想的一樣呢?"+

"\n不妨點擊一下顯示圖片按鈕,看圖更形象哦^_^");

//參數導入按鈕設置為可用

mainApplet.jButton1.setEnabled(true);

//顯示圖片按鈕設置為可用

mainApplet.jButton5.setEnabled(true);

//清除所有的密碼標籤內容

mainApplet.setNull();

}

}

我花錢買的

求解約瑟夫環問題(Java)

package 約瑟夫環;

import java.util.LinkedList;

import java.util.List;

/**

* 約瑟夫環問題的一種描述是:編號為1.2.3…….n的n個人按順時針方向圍坐一圈 ,每人手持一個密碼(正整數),

* 開始任意選一個整數作為報數上限值,從第一個人開始順時針自1開始順序報數,報到m時停止報數。報m的人出列,

* 將他的密碼作為新的m值,從他順時針下一個人開始重新從1開始報數,

* 如此下去直到所有的人全部都出列為止。試設計程序實現,按照出列的順序打印各人的編號。

* @author Administrator

*

*/

public class Question2 {

class person {

int password;

int number;

int state = 1;

public person(int password, int number) {

this.password = password;

this.number = number;

}

public person(int number){

this.number = number;

}

}

public int ListLength(Listperson list) {

int count = 0;

if (list != null) {

for (person p : list) {

if (p.state != 0) {

count++;

}

}

}

return count;

}

public void cacle() {

// 初始化數據

Listperson list = new LinkedListperson();

list.add(new person(3,1));

list.add(new person(1,2));

list.add(new person(7,3));

list.add(new person(2,4));

list.add(new person(4,5));

list.add(new person(8,6));

list.add(new person(4,7));

int position = -1;//初始位置

int m = 20; //第一次報多少的人出來

int count = 0;//已經報了多少人

while (ListLength(list) != 0) {

position = (position + 1) % list.size();// 位置定位

if (((person) list.get(position)).state != 0) {

count++;

}

if (count == m) {

person p = list.get(position);

System.out.print(p.number+" ");

p.state = 0;

m = p.password;

list.set(position, p);

count = 0;

}

}

}

public static void main(String[] args) {

Question2 q= new Question2();

q.cacle();

}

}

跟這差不多的。

怎麼用java數組實現約瑟夫環

用java數組實現約瑟夫環

packageJosephround;

publicclassJoseround{

intsit;

intflagjo=0;

Joseround(){};

Joseround(intx){

sit=x;

}

voidsetflag(intx){

flagjo=x;

}

}

packageJosephround;

publicclassInijose{

Joseroundjo[];

staticintlength=0;

Inijose(){};

Inijose(intx){

jo=newJoseround[x];

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

jo[i]=newJoseround(i+1);//創建對象數組

length++;

}

}

voiddelete(intn){

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

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

}

length–;

}

}

packageJosephround;

importjava.util.Scanner;

publicclassText{

publicstaticvoidmain(String[]args){

intm,n;

System.out.println("inputm");

Scannerm1=newScanner(System.in);

m=m1.nextInt();

System.out.println("inputn");

Scannern1=newScanner(System.in);

n=n1.nextInt();

inttemp=0;

intx=0;

Inijosejoseph=newInijose(n);

while(joseph.length!=0){

for(inti=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++;

elsex=0;

}

}

}

}

用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 + "]";

}

}

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/300193.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-29 12:51
下一篇 2024-12-29 12:51

相關推薦

  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean加載過程

    Java Bean加載過程涉及到類加載器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean加載的過程。 一、類加載器 類加載器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Python導入數組

    本文將為您詳細闡述Python導入數組的方法、優勢、適用場景等方面,並附上代碼示例。 一、numpy庫的使用 numpy是Python中一個強大的數學庫,其中提供了非常豐富的數學函…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Python返回數組:一次性搞定多種數據類型

    Python是一種多用途的高級編程語言,具有高效性和易讀性的特點,因此被廣泛應用於數據科學、機器學習、Web開發、遊戲開發等各個領域。其中,Python返回數組也是一項非常強大的功…

    編程 2025-04-29
  • Java判斷字符串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字符串中是否存在多個指定字符: 一、字符串遍歷 字符串是Java編程中非常重要的一種數據類型。要判斷字符串中是否存在多個指定字符…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29

發表回復

登錄後才能評論