Jun 24, 2009

It is a game. There are 1...N persons. the count 'M' is given. The M-th person is out of the game. The recounting starts again from M+1 th person. Fin

It is a game. There are 1...N persons. the count 'M' is given. The M-th person is out of the game. The recounting starts again from M+1 th person. Finally only one is remaining. He is the winner. Implement the algo.
list* foo(list* head, int n)
{
int idx=1;
while(head->next!=head)
{
if(idx==n)
{
tmp=head;
head->prev->next=head->next;
head->next->prev=head->prev;
head=head->next;
free(tmp);
idx=1;
}
else
{
head=head->next;
idx++;
}
}
return head;
}

No comments:

Post a Comment