Using structures and arrays to implement Joseph Ring (second of the problem of ring counting)

Source: Internet
Author: User

The day before yesterday with a single cycle linked list to achieve the Joseph ring problem, this method of implementation efficiency. This is followed by two other simple methods.

Method one: Using arrays

  

voidMain () {inta[Bayi],n,i,counter,num; Counter is used to calculate, num is used to record the number of exits printf ("Please input all the number of person :"); scanf ("%d",&N);  for(i=0; i<n;i++)//number for each person, 1~n {A[i]=i+1; }     for(i=0; i<n;i++)//print pre-status printf ("%d\t", A[i]); printf ("\ n"); Counter=0; i=0; num=0;  while(num< (n1) &&i<N) {if(a[i]!=0)//When the number is not 0 o'clock, the value plus 1 counter++; if((counter%3)==0&&a[i]!=0)//When the count value can be divisible by 3 and the element number is not 0 o'clock, the number value is placed 0,num plus 1****** this place at first did not judge A[i] is not equal to 0,
The result is a logic error {printf ("Number %d is out\n", A[i]); A[i]=0; Num++; if(num==n-1) Break; If NUM is the total number minus 1, then there is still one person. Roll out loop} I++; Loop Arrayif(i==N)//When the count is up to the last one, start the count again {i=0; }} printf ("\nthe last person is number"); Number of people remaining after the print operation for(i=0; i<n;i++) if(a[i]!=0) printf ("%d\t", A[i]);}

Method two: Using struct arrays

  

structperson{//Each element defines a member that holds the subscript nextp of the next element, a member holds the member numberintNumber ; intnextp;};voidMain () {intN out, Num,i; printf ("Please input the total number of the people:"); scanf ("%d",&N); structPerson Link[n];  for(i=0; i<n;i++) {if(i==n-1) link[i].nextp=0; The last person's next element position is the first element, forming a loop queueElselink[i].nextp=i+1; The next person's serial number Link[i].number=i+1; My number} printf ("\ n");  out=0; num=n-1; Define the number of out-logged exits, initialize num as the last element to make it easier to count off the first element while( out<n-1) {i=0;  while(i!=3) {num=LINK[NUM].NEXTP; Count off from the first elementif(Link[num].number) i++; When the number is not 0 o'clock, the Count plus 1} printf ("Number %d is out\n", Link[num].number); Print out the exit personnel number Link[num].number=0; Exit Person number 0 out++; } printf ("\nthe last person is number");  for(i=0; i<n;i++)        if(link[i].number!=0) printf ("%d\t", Link[i].number);}

 

Using structures and arrays to implement Joseph Ring (second of the problem of ring counting)

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.