"C" queuing problem

Source: Internet
Author: User

The title request is this: There are n people in a circle, in sequence, starting from the 1th person to count, from 1 to 3 count, usually check out 3 of the people out of the circle, after the number of people from 1 began to re-count, ask this circle the last remaining is the original number of the person.

This problem must be divided into two cases, that is, when the number of the queue is greater than 2 of the situation and equal to 2 of the case, of course, the number of people is 1 when the person is the answer. There are two cases to operate, because the count of 31 cycles, the number of people in the queue is greater than 2, the iterator traversing the array can continue to move backwards, if the number of people in the queue is 2, the iterator to the end of the need to reverse back to the beginning.

So the code looks like this:

#include <stdio.h>void main () {int a[255];int n;printf ("Number of Insiders:") scanf ("%d", &n);//array initialization int i;for (i=1;i<n +1;i++) {a[i]=i;} a[i]=0;//an array to seal int count=1;//The number of each person reported, counting off from 1 while ((a[2]!=0) && (a[3]!=0)) {//the team left 1 or 2 people to stop the algorithm for (i=1;a The process of [i]!=0;i++) {if (count%3==0) {//) is a process of shrinking the queue, which is the process of shrinking the array A. for (int j=i;a[j]!=0;j++) {a[j]=a[j+1];//count to 3 out of the team count=1;//can only be reported 1,2,3,1,2,3 .... 3, re-calculated from 1}}count++;//if it is reported 3, then report the number }}if (a[3]==0) {//If the array has only 2 digits left, the 2nd number is the answer, that is, if the entire queue has only 2 people left, then the 2nd person is the last person to count the a[1]=a[2]; printf ("The last person to count:%d\n", a[1]);//If the array has only 1 digits left, this is the digit answer, that is, if the entire queue has only 1 people, then this person is the last one to count off}

The results of the operation are as follows:


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C" queuing problem

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.