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