Algorithm: Joseph Ring Problem

Source: Internet
Author: User

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

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.