欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2014年03月21日 15:11:36   作者:  
這篇文章主要介紹了Java解決約瑟夫(環(huán))問題的代碼實(shí)例,決約瑟問題貌似經(jīng)常出現(xiàn)在面試題中,需要的朋友可以參考下

復(fù)制代碼 代碼如下:
package list;

import java.util.ArrayList;


/**
 * Java約瑟夫問題: n個人(不同id)圍成一個圈,從startId(任意數(shù))個開始報數(shù)m(任意數(shù))個數(shù),數(shù)m的人出列排成新隊(duì)列,m清零,
 * 然后又從下一個人開始數(shù)m個數(shù)開始,數(shù)到m就出列接在新隊(duì)列尾部,如此重復(fù),知道所有人都出列為止。
 * 打印 出列后的新隊(duì)列
 *
 * 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 + "]";
        }
    }
}


相關(guān)文章

  • spring?和?idea?建議不要使用?@Autowired注解的原因解析

    spring?和?idea?建議不要使用?@Autowired注解的原因解析

    @Autowired?是Spring框架的注解,而@Resource是JavaEE的注解,這篇文章主要介紹了spring和idea建議不要使用@Autowired注解的相關(guān)知識,需要的朋友可以參考下
    2023-11-11
  • 雙token實(shí)現(xiàn)token超時策略示例

    雙token實(shí)現(xiàn)token超時策略示例

    用于restful的app應(yīng)用無狀態(tài)無sesion登錄示例,需要的朋友可以參考下
    2014-02-02
  • 詳解@ConditionalOnMissingBean注解的作用

    詳解@ConditionalOnMissingBean注解的作用

    這篇文章主要介紹了詳解@ConditionalOnMissingBean注解的作用,@ConditionalOnMissingBean,它是修飾bean的一個注解,主要實(shí)現(xiàn)的是,當(dāng)你的bean被注冊之后,如果而注冊相同類型的bean,就不會成功,它會保證你的bean只有一個,需要的朋友可以參考下
    2023-10-10
  • mybatis如何使用truncate清空表

    mybatis如何使用truncate清空表

    這篇文章主要介紹了mybatis如何使用truncate清空表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 使用spring security明文密碼校驗(yàn)時報錯-BadCredentialsException: Bad credentials的問題

    使用spring security明文密碼校驗(yàn)時報錯-BadCredentialsException:&nbs

    小編遇到這樣一個問題在學(xué)習(xí)spring security時使用明文密碼進(jìn)行登錄校驗(yàn)時報錯"org.springframework.security.authentication.BadCredentialsException: Bad credentials,今天給大家分享問題原因及解決方案,感興趣的朋友一起看看吧
    2023-10-10
  • Spring Boot2.0中SpringWebContext找不到無法使用的解決方法

    Spring Boot2.0中SpringWebContext找不到無法使用的解決方法

    這篇文章主要給大家介紹了關(guān)于Spring Boot2.0中SpringWebContext找不到無法使用的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Intellij IDEA 斷點(diǎn)不可用報錯 No executable code found

    Intellij IDEA 斷點(diǎn)不可用報錯 No executable 

    這篇文章主要介紹了Intellij IDEA 斷點(diǎn)不可用報錯 No executable code found問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Java設(shè)計(jì)模式之裝飾器模式

    Java設(shè)計(jì)模式之裝飾器模式

    這篇文章介紹了Java設(shè)計(jì)模式之裝飾器模式,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09
  • JDK8通過Stream 對List,Map操作和互轉(zhuǎn)的實(shí)現(xiàn)

    JDK8通過Stream 對List,Map操作和互轉(zhuǎn)的實(shí)現(xiàn)

    這篇文章主要介紹了JDK8通過Stream 對List,Map操作和互轉(zhuǎn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java線程同步及實(shí)現(xiàn)方法詳解

    Java線程同步及實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Java線程同步及實(shí)現(xiàn)方法詳解,當(dāng)我們有多個線程要同時訪問一個變量或?qū)ο髸r,如果這些線程中既有讀又有寫操作時,就會導(dǎo)致變量值或?qū)ο蟮臓顟B(tài)出現(xiàn)混亂,從而導(dǎo)致程序異常,需要的朋友可以參考下
    2023-11-11

最新評論