View Code of Problem 80

#include<stdio.h>
#include<stdlib.h>
struct num
{
	int number;
	struct num *next;
}
main()
{
	struct num *p,*q,*head;
	int n,i,count;
	while(1)
	{
		q=NULL;
		scanf("%d",&n);
		if(n==0)break;
		for(i=1;i<=n;i++)
		{
			p=(struct num *)malloc(sizeof(struct num));
			if(q==NULL)
			{
				head=p;
				p->next=NULL;
				q=p;
				q->number=i;
			}
			else
			{
				q->next=p;
				q=p;
				q->number=i;
			}
			
		}
		q->next=head;
		p=q;
		q=head;
		count=0;
		while(1)
		{
			if(q->next==q)
				break;
			count++;
			if(count%3==0)
			{
				p->next=q->next;
				free(q);
				q=p->next;
			}
			else
			{
				q=q->next;
				p=p->next;
			}
		}
		printf("%d\n",q->number);
	}
}

Double click to view unformatted code.


Back to problem 80