13个人围城一圈报数,报到3退出圈子,就是猴子选大王问题,链表算法看不懂,求注释!!![em06]
13个人围城一圈报数,报到3退出圈子,就是猴子选大王问题,链表算法看不懂,求注释!!! 例如 如果是3个人报数 如123 则最后剩下2#include <stdio.h>
#define N 13
struct person
{ int number;
int nextp;
} link[N+1];
int main()
{ int i,count,h;
for (i=1;i<=N;i++)
{if (i==N)
link[i].nextp=1;
else
link[i].nextp=i+1;
link[i].number=i;
}
printf("\n");
count=0;
h=N;
printf("sequence that persons leave the circle:\n");
while(count<N-1)
{
i=0;
while(i!=3)
{
h=link[h].nextp;
if (link[h].number) i++;
} printf("%4d",link[h].number);
link[h].number=0;
count++;
}
printf("\nThe last one is ");
for (i=1;i<=N;i++)
if (link[i].numbe)
printf("%3d",link[i].number);
printf("\n");
return 0;
}
[ 本帖最后由 yz912498286 于 2015-9-16 09:15 编辑 ]