约瑟夫问题(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");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章