Topic:
13 people besieged a circle, starting from the first person to order the number of three-and-five. Where the registration of 3 people out of the circle. Find out the original serial number of the person who last left in the circle.
Analytical:
By test instructions, everyone will quit one by one, so that the last person to stay, you can think of the person to leave the mark, after the turn of a circle, will only leave a person, and then traverse all, not marked is the last person to stay in the circle.
Implementation: The code is as follows
#include <stdio.h>#include<stdlib.h>#defineN 13structperson{intNumber ; intNEXTP;} Link[n+1];voidCreastlink (structPerson link[]);intMain () {intb=0, K,j,i;//B (number of Exits), K (All-in-all), J (currently processed node number)creastlink (link); J=N; while((N-b) >1)//because to a circle of the count, one mark so think of while loop, cycle must have out of the loop conditions, that is, there is only one person in the circle to stop that (n-b) >1{k=0; while(k!=3) {J=LINK[J].NEXTP;//point to next node if(link[j].number) k++; } link[j].number=0; b++; } for(i=1; i<=n;i++) { if(link[i].number) printf ("%d", Link[i].number); } return 0;}voidCreastlink (structPerson link[]){ inti; for(i=1; i<=n;i++) {Link[i].number=i; } for(i=1; i<=n;i++) { if(i==N) {link[i].nextp=1; } Else{LINK[I].NEXTP=i+1; } } return;}
C Language Programming 9.6