27人转成一个圈,从第1个人开始顺序报号,凡报号为3和3的倍数者退出圈子,找出最后留在圈子中的原来的序号。要求用链表实现。
#include <stdio.h>
#include <stdlib.h>
typedef struct line
{
int nNum;
int m;
struct line *next;
}D;
int main()
{
struct line *ptemp,*head,*pnew,*ptempq,*pstore;
head=(struct line*)calloc(1,sizeof(struct line));
int i=0,j;
pnew=(D*)malloc(sizeof(D));//报错?????
head->next=pnew;
head->nNum=1;
ptemp=head->next;
for(i=0;i<25;i++)
{
ptemp->nNum=i+2;
pnew=(D*)malloc(sizeof(D));//报错?????
ptemp->next=pnew;
ptemp=ptemp->next;
}
ptemp->next=head;
ptempq=ptemp;
ptemp=head;
int count=27;
i=1;
while(1)
{
ptemp->m=i;
i++;
if((ptemp->m)%3==0)
{pstore=ptemp;
ptempq->next=ptemp->next;
ptemp=ptemp->next;
free(pstore);
count--;
if(count==1)
{
break;
}
}else{
ptempq=ptemp;
ptemp=ptemp->next;
}
}
printf("最后在圈中的人的序号为:%d\n",ptemp->nNum);
}