您的位置:首页 > 其它

约瑟夫问题

2015-10-16 21:52 239 查看
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *create(int n)
{
node *p=NULL,*head;
head=(node*)malloc(sizeof(struct node));
p=head;
node *s;
int i=1;
if(0!=n)
{
while(i<=n)
{
s=(node*)malloc(sizeof(node));//»¹±ØÐëÊÇ*node
s->data=i;
i++;
p->next=s;
p=s;
}
s->next=head->next;

}
free(head);
return s->next;
}

int main()
{
int n=41;
int m=3;
int i;
node *p=create(n);
node *temp;
m=m%n;
while(p!=p->next)
{
for(i=1;i<m-1;i++)
{
p=p->next;
}
printf("%d->",p->next->data);
temp=p->next;
p->next=temp->next;
free(temp);
p=p->next;

}
printf("%d\n",p->data);
getchar();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: