Algorithm: Joseph Ring Problem
"write in front."
Because I am born keenness, the code and description may be offensive, Master Please. But I have been trying to record a useful point of knowledge, give yourself to a friend, just hope to help Everyone.
"problem description"
Josef Ring (joseph Question) is a mathematical application problem: known n individuals (denoted by number 1,2,3...n) sit around a table. From the first person began to count, the number to m of the person out of the man, and his next person from 1 began to count, the number to m of the person and out of the way, according to this rule repeat, to the last circle of the Person's label.
Code
voidJOSEF (intNintM//:N individuals, m-dequeue{ intpeople[n+1];//we define an array size of n+1, because we do not want to involve 0 of the operation, we only take 1 to N in this range of labels, ignoring 0 for(intI=1; i<=n;i++) people[i]=i;//initializes each element of the array (except 0), in fact not 0, and the value does not matter. intflag=n-1;//How many elements do we want to delete in the array? The answer is a n-1. The one that's left for the last lap. intp=1;//used to indicate the element subscript intCount=0;//counter while(flag)//continue without deleting the case { if(people[p]==0)//if p refers to an element of 0, indicating that the element has been circled, you can skip { if(p==n)//This code is for the tail to connect the headp=1; ElseP++; Continue; } Count++;//Find a non-0 element if(count==m)//if we find enough m, we'll take this element out of the loop, which is set to 0.{people[p]=0; Flag--;//elements to be out of the loop-1Count=0;//Re-start number } if(p==n)//This code is for the tail to connect the headp=1; ElseP++; }
For (int i=1;i<n;i++)//that value is not 0 the subscript of the element is the number of the last person to be circled
{
If (people[i]!=0)
{printf ("target is%d\n", i); break;}
}
Algorithm: Joseph Ring Problem