Two implementations of Joseph's ring

Source: Internet
Author: User

Array Implementation and linked list implementation, I do not understand the formula, tragedy!

The linked list method is the simplest. Initialize a circular linked list and naturally form a Joseph ring. You do not need to move any elements. You only need to delete the M elements in sequence until the linked list is empty.

Code

# Include <iostream> # include <algorithm> using namespace STD; typedef struct node {int data; struct node * Next;} node, * List; List createlistwithouthead (int n) // create a circular linked list with no leading nodes {If (n = 0) return NULL; List head = (list) malloc (sizeof (node )); head-> DATA = 1; head-> next = NULL; list p = head; List q = NULL; For (INT I = 2; I <= N; I ++) {q = (list) malloc (sizeof (node); q-> DATA = I; q-> next = NULL; P-> next = Q; P = Q ;} p-> next = head; // the end A pointer points to the first node return head;} void juseph (list head, int N, int m) {int COUNT = 0; list p = head, q = NULL; for (INT I = 0; I <n; I ++) // There are n people in total {COUNT = m; while (-- count) // pick out the M-th individual {q = P; P = p-> next;} cout <p-> data <""; q-> next = p-> next; // Delete the M-th individual P = Q-> next; // count from the next person} void traversewithouthead (list head) // traverse the linked list with no leading node {If (Head = NULL) return; list p = head; do {cout <p-> data <""; P = p-> next;} while (P! = Head); cout <Endl;} int main () {int n = 3; List head = createlistwithouthead (n); traversewithouthead (head); juseph (Head, 3, 5 ); getchar (); Return 0 ;}

If array representation is followed by the previous question, the elements need to be moved every time. Here, we can trick you into applying for a number with a length of N. Each element is initialized as a non-zero element. If this element has been removed, set it to 0, when counting, only one element is counted. The ring is simulated by I = (I + n + 1) % N.

Code

# Include <stdio. h> # include <malloc. h> # include <stdlib. h> int main () {int * P; int M, N; printf ("input m and n \ n"); scanf ("% d", & M, & N); fflush (stdin); P = (int *) malloc (sizeof (INT) * m); For (INT I = 0; I <m; I ++) // create an array with the array element numbered. If a person leaves the queue, the number is 0 p [I] = I + 1; int COUNT = 0; // int sum = 0 when reporting data; // calculate the number of players in the team. int K = 0; // record the subscript of the current element while (sum <m) // loop until m members are both out of the queue {If (P [k] = 0) // if the person in this position has already been out of the queue, skip and do not count, that is, Count does not change {k = (k + m + 1) % m; // skip this position} else // if the person in this position is not in the queue {count ++; // Add 1 to count, if (COUNT = N) // if n is reported, this person will be outputted {printf ("% d ", P [k]); P [k] = 0; // after leaving, set the element in this position to zero sum ++; // Total number of people leaving the team + 1 COUNT = 0; // The report counter starts from scratch again} k = (k + m + 1) % m ;}} system ("pause ");}

Related Article

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.