首页 > 其他 > 详细

循环链表模版--约瑟夫

时间:2014-04-18 05:27:17      阅读:532      评论:0      收藏:0      [点我收藏+]

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int n,m;
typedef struct node
{
    int date;
    struct node *next;
}node;

int main()
{
    int i,sum=0,count=0;
   scanf("%d%d",&n,&m);
   node *tail,*head,*p,*q;
   head=(node *)malloc(sizeof(node));
   head->date=1;
   head->next=NULL;
   tail=head;
   for(i=2;i<=n;i++)
   {
       p=(node *)malloc(sizeof(node));
       p->date=i;
       p->next=NULL;
       tail->next=p;
       tail=p;
   }
   tail->next=head;
   for(q=head;q->next!=head;)
     q=q->next;
   while(count<n-1)
   {
       p=q->next;
       sum++;
       if(sum%m==0)
       {
           q->next=p->next;
           free(p);
           count++;
       }
       else q=p;
   }
   printf("%d\n",q->date);


  return 0;
}

循环链表模版--约瑟夫,布布扣,bubuko.com

循环链表模版--约瑟夫

原文:http://www.cnblogs.com/zhangmingcheng/p/3671855.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!