約瑟夫問題(Josephu)

問題描述

設編號分別爲:1,2,…,n的n個人圍坐一圈。約定序號爲k(1 <= k < = n)的人從1開始計數,數到m的那個人出列,他的下一位又從1開始計數,數到m的那個人又出列,依次類推,直到所有人出列爲止。

算法思路

用一個不帶頭結點的循環鏈表來處理Josephu問題:先構成一個有n個結點的單循環鏈表,然後從第k結點起從1計數,計到m時,對應結點從鏈表中刪除;然後再從被刪除結點的下一個結點起又從1開始計數….,直到所有結點都列出時算法結束。

C 解法

#include <stdio.h>
#include <stdlib.h>

typedef struct _node_
{
    int data;
    struct _node_ *next;

}ListNode;

ListNode* get_node(int data)
{
    ListNode *temp;

    temp = (ListNode *)malloc(sizeof(ListNode));
    temp->data = data;

    return temp;
}

int insert_data(ListNode **p,ListNode *q)
{
    static ListNode *head;

    if(*p == NULL)
    {
        *p = q;
        head = q;

    }else{

        (*p)->next = q;
        q->next = head;
        //改變移動指針
        *p = q;
    }

    return 0;
}

ListNode *create_loop(int m)
{
    int i = 0;
    ListNode *temp = NULL;
    ListNode *pmove = NULL;

    for(i = 1;i <= m;i ++)
    {
        temp = get_node(i);
        insert_data(&pmove,temp);
    }

    return pmove;
}

int delete_data(ListNode **p,ListNode *q)
{
    (*p)->next = q->next;
    free(q);
    q = NULL;

    return 0;
}

int main()
{
    ListNode *p,*q,*temp;
    int i = 0;
    int n,k,m;

    printf("input n k m:");
    scanf("%d%d%d",&n,&k,&m);
    while(getchar()!= '\n');

    //創建約瑟夫環
    p= create_loop(n);

    //第一個人的位置
    q = p->next;

    //找到地k個人
    for(i = 1;i < k;i ++)
    {
        p = p->next;
        q = q->next;
    }

    while(q != p)
    {    
        for(i = 1;i < m;i ++)
        {
            p = p->next;
            q = q->next;
        }

        printf("%d ",q->data);
        delete_data(&p,q);
        q = p->next;
    }

    printf("%d\n",p->data);

    return 0;
}

java 解法

package com.chen;
/**
 * 丟手帕問題
 * @author chenwen
 *
 */
public class Josephu {
    public static void main(String []args){
        CycLink cyclink = new CycLink();
        cyclink.setLen(6);
        cyclink.creatLink();
        cyclink.show();
        cyclink.setK(1);
        cyclink.setM(2);
        cyclink.play();
    }
}
//child node
class Child{
    int no; //number
    Child nextChild = null;
    public Child(int no){
        this.no = no;
    }
}
//環形鏈表
class CycLink{
    //先定義一個指向鏈表第一個小孩的引用
    Child firstChild = null;//指向第一個小孩的,不能動
    int len = 0;//表示共有幾個小孩
    int k;//從第幾個開始數
    int m;//每次數多少下

    public void setLen(int len){
        this.len = len;
    }
    public void setK(int k){
        this.k = k;
    }
    public void setM(int m){
        this.m = m;
    }
    public void creatLink(){
        Child temp = null;
        for(int i = 1;i <= len; i++){
            //先創建第一個小孩
            if(i == 1){
                Child ch = new Child(i);
                this.firstChild = ch;
                temp = ch;
            }else if (i == len){    //最後一個小孩
                //繼續創建小孩
                Child ch = new Child(i);
                temp.nextChild = ch;
                temp = ch;
                temp.nextChild = this.firstChild;
            }else{
                //繼續創建小孩
                Child ch = new Child(i);
                temp.nextChild = ch;
                temp = ch;
            }
        }
    }
    public void play(){
        Child temp = null;
        //1.先找到開始數的人,第k個人
        temp = this.firstChild;
        for(int i = 1;i < k;i++){
            temp = temp.nextChild;
        }
        while(this.len > 0){
        //2.數m-1下
        //這裏有個問題,就是,要刪除第m個小孩,需要將第m-1個小孩的nextchild指向m+1個小孩。
        //所以,我們只需找到第m-1個小孩,然後,刪除下一個小孩就行了。
        //首先m的值不能爲0,因爲每次數0個,小孩永遠都不會退出。
        //m >= 1;
        //當m == 1 時候,我們需要數0個;也就是不數
            for(int j = 1;j < m-1;++j){     //  數m-1個小孩
                temp = temp.nextChild;          
            }
            //3.將第m個小孩退出
            System.out.println("out:" + temp.nextChild.no);
            if(temp.nextChild == this.firstChild){      //此處一定要注意!!因爲如果不設置這個的話,以後遍歷無法遍歷
                this.firstChild = temp.nextChild.nextChild;
            }
            temp.nextChild = temp.nextChild.nextChild;
            temp = temp.nextChild;
            this.len--;
            this.show();
        }
    }
    public void show(){
        if(this.len == 0){
            System.out.println("鏈表爲空");
            return;
        }
        Child temp = this.firstChild;
        for(int i = 1;i <= this.len;++i){
            System.out.print(temp.no + " ");
            temp = temp.nextChild;
        }
        System.out.println("\n");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章