Implementation of Joseph's ring game of data reporting

Source: Internet
Author: User

During the interview, you are often asked to report games. Implement it.

Problem description:
There are n people sitting around and starting from 1 to n in clockwise direction, starting from the second person 1 to M reports, when the number is up to m people, this person goes out of the circle, then start the 1-M report from his next person, so proceed until all people are out of the circle. Now we need to print out the circle order.

Input: N game total number S start number M Value
Output: P points to an array with N length, and the outbound order is stored in the array pointed to by P.
Example
N = 7 S = 2 m = 3 outbound order: 4 7 3 1 6 2 5
N = 3 S = 1 m = 2 outbound order: 2 1 3

Use a circular linked list to implement the following:

 # include 
  
    using namespace STD; typedef struct tagnode {int data; struct tagnode * Next;} node; void Joseph (INT count, int start, int steps) {node * P, * q, * head; head = (node *) New node; P = head; for (INT I = 1; I <= count-1; I ++) {P-> DATA = I; P-> next = (node *) New node; P = p-> next;} p-> DATA = count; // The final special handling of the last player to form a circular linked list p-> next = head; P = head; while (p-> next! = P) {While (p-> data! = Start) P = p-> next; If (Steps = 1) {for (INT I = 1; I <= count; I ++) {cout 
   
     data <"\ t"; P = p-> next;} cout 
    
      next; // P points to the first member to be listed} q = p-> next; cout 
     
       data <"\ t"; // Q is the member to be listed p-> next = Q-> next; // reconnects the linked list delete Q; start = p-> next-> data; // P indicates that the next player is the first player in the next round.} cout 
      
        data; // output the last column member Delete P; cout 
       
         count> Start> steps; Joseph (count, start, steps); Return 0 ;}
       
      
     
    
   
  

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.