Joseph's problem-circular linked list

Source: Internet
Author: User

After the Roman occupied jotabat, 39 Jews hid in a cave with Joseph and his friends. 39 Jews decided to die rather than be caught by the enemy, so they decided to commit suicide, 41 people are arranged in a circle, with 1st people reporting data. Each time the number reaches 3rd, the person must commit suicide, and then report again from the next one until all people commit suicide. However, Joseph and his friends do not want to follow suit. First start from a person, cross the K-2 individual (because the first person has been crossed), and killKIndividual. Next, go over the K-1 and killKIndividual. This process continues along the circle until only one person remains. The problem is, given the sum, where should we start to avoid being executed? Joseph asked his friends to pretend to follow the game. He arranged his friends and himself in 16th and 31st positions, so he escaped the game of death.

Now let's generalize the problem: N people are in a circle, starting from the first to report the number, the m will be killed, and their death order will be output.

N = 41, M = 3:

Declare a struct, the data field records its serial number, and the pointer field points to the next node. Point the pointer P to the first node location, P = p-> next, (move one after), delete the node temp at the position indicated by P-> next, and then execute

P-> next = p-> next, output the temp data, and then p = p-> next (move to the fourth node )....... such a reciprocating Solution

Generally:

When M = 4, P = p-Next is executed twice and then temp is found. When M = 5, P = p-> next is executed three times to find temp ....... For the general m, the next position of the (M-2) pointer after the displacement is very much to delete the location

# Include <stdio. h> # include <stdlib. h> typedef struct node {int data; struct node * Next;} node; node * Create (INT m) {int I = 1; node * head, * tail, * s; head = (node *) malloc (sizeof (node); tail = head; If (M! = 0) {While (I <= m) {S = (node *) malloc (sizeof (node); s-> DATA = I ++; tail-> next = s; tail = s;} s-> next = head-> next;} Free (head); Return S-> next;} int main (void) {int M, N; biaoqian: scanf ("% d", & M, & N); node * P, * temp; P = create (m ); int I; while (P! = P-> next) {P = p-> next; for (I = 1; I <N-2; I ++) P = p-> next; temp = p-> next; // remember not to convert it into P-> next = temp. In this way, the linked list breaks down p-> next = temp-> next; printf ("-> % d", temp-> data); free (temp); P = p-> next ;} printf ("-> % d \ n", p-> data); If (getchar ()! = '#') {Goto biaoqian;} return 0 ;}

Running result



Joseph's problem-circular linked list

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.