分析:
建立一个环状链表,然后按照每n个数删掉一个结点,输出最后剩余的结点的数值。
#include <stdio.h>
#include <malloc.h>
struct node
{
int date;
struct node *next;
};
struct node *create(int NUM)
{
struct node *Head,*p,*tail;
int i;
Head = (struct node*)malloc(sizeof(struct node));
Head->next = NULL;
tail = Head;
for(i = 1;i <= NUM;i++)
{
p = (struct node*)malloc(sizeof(struct node));
p->next = NULL;
p->date = i;
tail->next = p;
tail = p;
}
p->next = Head->next;
return Head;
}
void del(struct node *Head,int NUM1,int NUM2)
{
int i ,sum = 0,Date;
struct node *p,*q;
p = Head->next;
q = Head;
i = 1;
while(sum < NUM1-1)
{
if(i % NUM2 == 0)
{
q ->next = p->next;
p = p->next;
i = i+1;
sum = sum + 1;
}else
{
q = q->next;
p = p->next;
i++;
}
}
Date = q->date;
printf("%d\n",Date);
}
int main()
{
struct node *head;
int num1,num2;
scanf("%d %d",&num1,&num2);
head = create(num1);
del(head,num1,num2);
return 0;
}