一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務(wù)器之家 - 編程語言 - JAVA教程 - Java解決約瑟夫問題代碼實(shí)例

Java解決約瑟夫問題代碼實(shí)例

2019-11-15 14:41java教程網(wǎng) JAVA教程

這篇文章主要介紹了Java解決約瑟夫(環(huán))問題的代碼實(shí)例,決約瑟問題貌似經(jīng)常出現(xiàn)在面試題中,需要的朋友可以參考下

代碼如下:

package list;

 

import java.util.ArrayList;


/**
 * Java約瑟夫問題: n個人(不同id)圍成一個圈,從startId(任意數(shù))個開始報數(shù)m(任意數(shù))個數(shù),數(shù)m的人出列排成新隊列,m清零,
 * 然后又從下一個人開始數(shù)m個數(shù)開始,數(shù)到m就出列接在新隊列尾部,如此重復(fù),知道所有人都出列為止。
 * 打印 出列后的新隊列
 *
 * eg
 * int n = 10;//總?cè)藬?shù)
 * int m = 3;   //報數(shù)個數(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; //報數(shù)個數(shù)
        int startIndex = 12; //起點(diǎn)位置
        System.out.println("JosephListTest: n= " + n + ", m= " + m +
            ", startIndex= " + startIndex + "\n\nQUEUE RESULT:");

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

        //list.search();
        if ((list != null) && (list.head != null)) {
            queueList = new ArrayList<JosephListTest.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 + "]";
        }
    }
}

 

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲a在线视频 | 成人精品免费网站 | 草草免费观看视频在线 | 久热人人综合人人九九精品视频 | 成人精品在线 | 天天干天天操天天爽 | 精品午夜中文字幕熟女人妻在线 | 色啊色 | 欧美透逼视频 | 国产精品香蕉在线观看不卡 | 女教师雪白老汉 | 色欲都市 | 色综合天天综合网国产人 | 日本一卡二卡3卡四卡网站精品 | zoz.zzz色| 国产精品欧美在线观看 | 亚洲欧美日韩精品高清 | 国产草草视频 | 7777奇米影视 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | 国产精品污双胞胎在线观看 | 国产成人一区二区三区小说 | 天天做天天爱天天综合网 | 6080窝窝理论 | bt天堂午夜国产精品 | 九9热这里只有真品 | 精品一区二区三区在线成人 | 青草青草久热精品视频在线网站 | 高清女主播一区二区三区 | 美女被扒开屁股进去网 | 色综合伊人色综合网站中国 | 欧美巨胸 | 国产实拍会所女技师在线 | 男人添女人 | 好涨好大我快受不了了视频网 | 舔穴吸奶 | 精品久久久久中文字幕日本 | 草草在线视频 | 亚洲日本aⅴ片在线观看香蕉 | 五月天导航| 娇妻被老外疯狂调教 |