C Language Programming 9.6

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.