13、 耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛


运行有点差错,不知道错在哪,待解决



#include
#include

typedef struct people
{
int number;
struct people *next;
}people, *ptrpeople;

int main()
{
ptrpeople phead = (ptrpeople)malloc(sizeof(people));
ptrpeople ptemp = phead;
for (int i = 0; i < 12; i++)
{
ptrpeople ptail = (ptrpeople)malloc(sizeof(people));
ptemp->next = ptail;
ptemp->number = i + 1;
ptemp = ptail;
}

ptemp->next = phead;
ptemp->number = 13;
int count = 1;
int delete_number = 0;
ptrpeople psave;
do
{
phead = phead->next;
count++;
if (count%2== 0)
{
psave = phead->next;
phead->next = phead->next->next;
count++;
free(psave);
delete_number++;
}

} while(delete_number!=12);
printf("叛徒的编号是:%d",phead->number);
return 0;

}


运行结果:

13、 耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛_第1张图片

你可能感兴趣的:(从零学习c语言编程题)