#include<stdio.h>
#include <stdlib.h>
#include <string.h>
struct link
{
int n;
struct link *next;
};
struct link *creat(int n)//创造环形链表(循环)
{
int i;
struct link *p,*q,*head=NULL;
for(i=1;i<=n;i++)
{
p=(struct link *)malloc(sizeof(struct link));
if(head==NULL)
head=p;
else
q->next=p;
p->n=i;
q=p;
}
p->next=head;
return (head);
}
main()
{
struct link *h,*q,*r;
int num;
scanf("%d",&num);
h=creat(num);
int count=1;
q=r=h;
while(q->next!=r)//筛选
{
if(count%3!=0)
{
r=q;
q=q->next;
}
else
{
q=q->next;
r->next=q;
}
count++;
}
printf("%d\n",q->n);
}
N人报数,数到3退出(约瑟尔问题)
最新推荐文章于 2021-05-20 00:22:30 发布
