当前位置:  编程技术>java/j2ee

Java解决约瑟夫问题代码实例

    来源: 互联网  发布时间:2014-11-04

    本文导语:  代码如下:package list; import java.util.ArrayList; /** * Java约瑟夫问题: n个人(不同id)围成一个圈,从startId(任意数)个开始报数m(任意数)个数,数m的人出列排成新队列,m清零, * 然后又从下一个人开始数m个数开始,数到m就出列...

代码如下:
package list;

import java.util.ArrayList;


/**
 * Java约瑟夫问题: n个人(不同id)围成一个圈,从startId(任意数)个开始报数m(任意数)个数,数m的人出列排成新队列,m清零,
 * 然后又从下一个人开始数m个数开始,数到m就出列接在新队列尾部,如此重复,知道所有人都出列为止。
 * 打印 出列后的新队列
 *
 * 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:");

        ArrayList 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 queuePreson(int n, int m, int startIndex) {
        ArrayList queueList = null;
        PersonList list = createList(n);

        //list.search();
        if ((list != null) && (list.head != null)) {
            queueList = new ArrayList();

            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 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 = 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 实现约瑟夫环的实例代码
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java Servlet获取和设置cookie实例代码
  • 万般火急!关于java打印,已经得到printerJob实例,那么怎么通过它得到Pageable实例?
  • 可以有其他两个类的实例同时调用一个java实例的两个方法吗?
  • <java技术手册>与<java实例技术手册>这两本书怎么样?
  • Java单例模式实例简述
  • 寻求java加密算法及实例
  • java web start实例代码COPY不了,怎么办?
  • 请问哪里有《java实例技术手册》的电子书下载?100分赠送!
  • 请教:JAVA中说什么类的实例,那是怎么样的一个概念呢?
  • java实现大数加法(BigDecimal)的实例代码
  • Java究竟能干些什么呢?清高手们列举一些实例出来,跟帖有分.
  • java HashMap的keyset实例
  • java获取当前日期使用实例
  • java之super关键字用法实例解析
  • Java调用DOS实现定时关机的实例
  • java结束进程的实例代码
  • 急!大家谁有类似visio的java实例或代码?
  • java 如何获取对象实例的大小
  • 高分火速求解,请在线朋友回答:java自定义类怎样生成实例数组?( className[] N=new className[X];怎么不行?)
  • 刚学java想试编一个文本编辑器,各位能不能给推荐一些较好的参考程序或实例
  • Java位运算和逻辑运算的区别实例
  • java命名空间java.sql类types的类成员方法: java_object定义及介绍
  • 我想学JAVA ,是买THINK IN JAVA 还是JAVA2核心技术:卷1 好???
  • java命名空间java.awt.datatransfer类dataflavor的类成员方法: imageflavor定义及介绍
  • 请问Java高手,Java的优势在那里??,Java主要适合于开发哪类应用程序
  • java命名空间java.lang.management类managementfactory的类成员方法: getcompilationmxbean定义及介绍
  • 如何将java.util.Date转化为java.sql.Date?数据库中Date类型对应于java的哪个Date呢
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getlibrarypath定义及介绍
  • 谁有电子版的《Java编程思想第二版(Thinking in java second)》和《Java2编程详解(special edition java2)》?得到给分
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getstarttime定义及介绍
  • 本人想学java,请问java程序员的待遇如何,和java主要有几个比较强的方向


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3