[Algorithm design] Joseph Ring

Source: Internet
Author: User

Description of the problem Joseph (Joeph) is described as follows: numbers 1, 2 ,..., N people in n circle clockwise, each holding a password (positive integer ). Select a positive integer from the beginning as the maximum value of m. Start from the first person and start from 1 clockwise. when reporting to m, stop reporting. The person who reported m is listed, and his password is used as the new m value, starting from the next person in the clockwise direction to record from 1 until all the people are listed. Design a program to find out the column sequence. It is basically required to use a one-way cyclic linked list storage structure to simulate this process. The key to game implementation is the storage of game information. The information includes the gamer's seat information, gamer's reported number information, and password information. We use a custom one-way cyclic linked list Joeph_list storage structure to simulate the game process. The linked list is connected by nodes. Node storage information includes the password of each person's hand, the location of each person, the storage location of the next Node in the computer, and the pointer to the next Node. It is worth noting that the information "location of each person" is essential because it is not the same as the position of the node in the linked list-but after a player is removed, the element position after the linked list is "forward", and the player position we need remains unchanged. The number of player reports is achieved through the increasing counter in the loop. When the order increases to the last node in the linked list, and the loop is still not over, we continue to increase from the first element-and start reporting from the first player if the last player has not reported the number to m. Until the counter is accumulated to m, we will find the node to be removed, record and output the information of the removed node, and continue the game. The program ends until the elements in the linked list are cleared. The key to the www.2cto.com algorithm is to abstract the actual game scenario to the search and removal of elements in the linked list. You need to know which data represents and which information, and be familiar with the various judgment processes in the program running. Algorithm flow data structure in this game, assuming that everyone is a node, this is conducive to the understanding of the program. [Cpp] template <class List_entry> struct Node {List_entry code; // stores the password List_entry iposition in the hands of each person; // stores the Node <List_entry> * next; node (); Node (List_entry a, List_entry B, Node <List_entry> * link = NULL) ;}; template <class List_entry> class Joeph_list {public: Joeph_list (); int size () const; bool empty () const; void clear (); Error_code retrieve (int position, List_entry & x, List_entry & y) cons T; Error_code replace (int position, const List_entry & x, const List_entry & y); Error_code remove (int position, List_entry & x, List_entry & y); Error_code insert (int position, const List_entry & x, const List_entry & y );~ Joeph_list (); protected: int count; void Init (); // initialize the linear table Node <List_entry> * head; Node <List_entry> * set_position (int position) const; // return the pointer pointing to the position Node}; Node Structure: indicates the Node Joeph_list and List table are implemented: data structure List class for storing players' seats, passwords, and other information in the game: stores images and other data structures in the form of a linked List. Global Object: game: SimpleWidow's window outputs the game process List <BitMap> tu; list <BitMap> shu; List <BitMap> people; stores the reports of game participants, passwords, and images of game participants. Global function: void Baoshu (int p, int s); used to display the effect of the number of game participants void Yizou (int p, int m ); the void Code (int m) of the game participant that is used to remove the reported number; the void Jieshu () is used to update the password information; the game project test ends; 1. The game starts. The initial m value is 6, the first player starts to report the number automatically, and the number of people who report the report is listed. 2. Continue the game with the password in the column as the password (not greater than 6). 3. Continue the game project demonstration until all players are listed.

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.