注册 登录
编程论坛 数据结构与算法

【求助】关于Josephu问题

li8311559 发布于 2010-12-13 20:48, 448 次点击
1)问题描述
Josephu问题为:设编号为1,2,… n的n个人围坐一圈,约定编号为k(1<=k<=n)的人从1开始报数,数到m 的那个人出列,它的下一位又从1开始报数,数到m的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
(2)输入与输出
输入:人数n和整数m
输出:出队编号的序列
提示:用一个不带头结点的循环链表来处理Josephu 问题:先构成一个有n个结点的单循环链表,然后由k结点起从1开始计数,计到m时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从1开始计数,直到最后一个结点从链表中删除算法结束。此题还可以用一维数组来实现,想想应如何实现。还可以归纳总结出一般性规律,看看该问题是否能够用数学上的取摸运算来实现。




#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
         int date;
         struct node *next;
}LNode,*LinkList;
LinkList creat(int n)/*建立循环单链表*/
{
          LinkList L=NULL;
          LNode *s,*r=NULL;
          int i;
          for(i=1;i<=n;i++)
          {
                           s=(LNode*)malloc(sizeof(LNode));
                           s->date=i;
                           if(L==NULL)L=s;
                           else r->next=s;
                           r=s;
          }
          r->next=L->next;
          return L;
}
void josephu(LinkList L,int k,int m)/*k指约定的编号,m指出对的数*/
{
                  LNode *p=L->next,*q;
                  int i=1;
                  while(p&&i<k)
                  {            
                               i++;

                     p=p->next;
                              
                  }
                  i=1;
                  while(p&&i<m)
                  {
                               p=p->next;
                               i++;
                               if(i==m)
                               {
                                       printf("%d",p->date);
                                       q=p->next;
                                       p->next=q->next;
                                       free(q);
                                       p=p->next;
                                       i=1;
                               }
                  }
}
main()
{
       LinkList L;
       int n,k,m;
       printf("输入人数");
       scanf("%d",&n);
       printf("输入开始位置");
       scanf("%d",&k);
       printf("输入出对数");
       scanf("%d",&m);
       L=creat(n);
       josephu(L,k,m);
       system("pause");
}      





我写的程序死循环,到底哪错了
                 
                              
1 回复
#2
寒风中的细雨2010-12-16 08:38
程序代码:
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
         int date;
         struct node *next;
}LNode,*LinkList;

LinkList creat(int n)/*建立循环单链表*/
{
    LinkList L=NULL;
    LNode *s,*r=NULL;
    int i;
    for(i=1;i<=n;i++)
    {
        s=(LNode*)malloc(sizeof(LNode));
        s->date=i;
        s->next = NULL;
        if(L==NULL)
            r = L = s;
        else
            r->next=s;
        r=s;
    }
    //r->next=L->next;
    r->next = L;
   
    return L;
}
LinkList search( LinkList L, LinkList l )
{
    LinkList temp = L;
    while ( l != temp->next )
        temp = temp->next;

    return temp;

}
void josephu(LinkList L,int k,int m)/*k指约定的编号,m指出对的数*/
{
    LNode *p=L,*q, *f;
    int i=1;
    while(i<k)
    {           
        i++;
        p = p->next;                     
    }
    i=1;               
    //while(p&&i<m)                 
    while(p != p->next)
    {                           
        if(i==m)
        {
            f = search(L, p);
            printf("%d",p->date);
            q = p;
            p = p->next;
            f->next = p;
            i=1;
            free(q);
        }
        else
        {
            p=p->next;
            ++i;
        }
    }
    printf("%d",p->date);
}

int main()
{
       LinkList L = NULL;
       int n,k,m;
       printf("输入人数");
       scanf("%d",&n);
       printf("输入开始位置");
       scanf("%d",&k);
       printf("输入出对数");
       scanf("%d",&m);
       L=creat(n);
       josephu(L,k,m);
       system("pause");

       return 0;
}      

/*我写的程序死循环,到底哪错了*/
1