首页 > 编程知识 正文

约瑟夫环java数组

时间:2023-12-29 13:16:50 阅读:330191 作者:JTGJ

本文目录一览:

高分求解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数组实现约瑟夫环

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约瑟夫问题: 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 + "nnQUEUE 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 + "]";

}

}

}


版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。