题目:13个人围成一圈,从第一个人开始顺序报号1,2,3。凡报到“3”者退出圈子。找到最后留在圈子里的人原来的序号(用链表实现)
#include
#define N 13
struct person
{
int number;
int nextp;
}link[N];
int main()
{
int i = 0;
int count = 0;
int h = 0;
for(i = 1; i <= N; i++)
{
if(i==N)
link[i].nextp = 1; //循环链表
else
link[i].nextp = i+1; // point to next
link[i].number = i;
}
h = N; //
while(count < N-1)
{
i = 0;
while(i != 3)
{
h = link[h].nextp;
if(link[h].number != 0)
i++ ;
}
printf("%d\t",link[h].number);
link[h].number = 0; // 到3 计0
count++;
}
printf("\n");
printf("The last one is");
for(i = 0; i <= N; i++ )
{
if(link[i].number)
printf("%3d",link[i].number);
}
printf("\n");
}
=========================================================================
3 6 9 12 2 7 11 4 10 5 1 8
The last one is 13
阅读(952) | 评论(0) | 转发(0) |