The question of "C" n Individuals in a circle

Source: Internet
Author: User

Question from: "C Program Design" (fourth edition) Rectification Tsinghua University Press NO. 291 page

There are n individuals besieged in a circle, order automatic arranging. Starting from the first person (from 1 to M), the person who reports to M exits the circle and then counts off from the next one in the loop queue (the number is 1). The last one left was the number of the original.

Note: This example is a simulation problem, according to the requirements of the program can be written.

Example code:

1#include <stdio.h>2 3 inta[ -];4 5 intFindint*a,intNintm)6 {7     intCounter =N;8     intI=0, j=0;9      while(Counter! =1)//When there's one left, you jump out of circulationTen     { One          for(j=1;j<=m;)//Count off from 1, report to M. A         { -i = (i++)%N; -             if(A[i]! =0) the             { -J + +; -             } -         } +A[i] =0;//kick the man who reported m to the queue -counter--;//Counter-1 +     } A      for(i=0; i<n;i++) at     { -         if(A[i]! =0) -             returnI//returns the location of the only remaining person -     } - } -  in  - //Test to intMain () + { -     inti; the      for(i=0;i< -; i++) *     { $A[i] =i;Panax Notoginseng     } -printf"%d", find (A,5,3)); the     return 0; +}

The question of "C" n Individuals in a circle

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.