Joseph's Problem Solving (C ++)

Source: Internet
Author: User

 

There are n people standing in one circle, and each person holds a password (positive integer ). Starting from the person t, clockwise "1, 2, 4 ,..." The number of cyclically reported messages is counted to M1 (the number of people holding the password T), and then the number of reported messages starts from the next person in the column, and the number of messages to M2 (the password of the person holding the column) again, until n people are listed.

The problem is: For any given original order of N people, find the order of N people in the column.

The input data is read from the text file. There are two lines in the file: Row 1st has only one integer, indicating the start position of the report number; row 2nd contains N passwords.

The output result is displayed on the screen.

For example, reading data from a text file

2

5 6 3 2 2 4

Screen Display

1
6 5 3
4 2

The procedure is as follows:

# Include <iostream> <br/> # include <fstream> <br/> # include <string> <br/> using namespace STD; <br/> // node class <br/> class node {<br/> Public: <br/> // constructor <br/> node (int x, int num, node * node = NULL) {<br/> stride = x; <br/> next = node; <br/> Number = num; <br/>}< br/> int stride, number;/* The m value of Stride Joseph's problem. number is the serial number of a person */<br/> node * next; <br/>}; <br/> // circular linked list <br/> class circlelist {<br/> Public: <br/> // Constructor Number <br/> circlelist () {<br/> first = last = NULL; <br/> length = 0; <br/>}< br/> // Insert a new node to the end of the linked list <br/> void insertlast (INT stride, int number) {<br/> If (first = NULL) {<br/> first = last = new node (stride, number); <br/> first-> next = first; <br/>}< br/> else {<br/> last-> next = new node (stride, number, first ); <br/> last = last-> next; <br/>}< br/> length ++; <br/>}< br/> // Delete the next node of a node <br/> void removenex T (node * node) {<br/> node * temp = node-> next; <br/> If (first = last) <br/> first = last = NULL; <br/> else <br/> node-> next = node-> next; <br/> Delete temp; <br/> length --; <br/>}< br/> // search nodes by serial number <br/> node * getnode (INT rank) {<br/> node * t = first; <br/> for (INT I = 1; I <rank; I ++) {<br/> T = T-> next; <br/>}< br/> return t; <br/>}< br/> node * First, * last; <br/> int length; <br/>}; <br/> int Mai N () {<br/> circlelist list; <br/> int start = 0; <br/> string filename; </P> <p> // open the file <br/> cout <"Enter the file name: (data.txt)" <Endl; <br/> CIN> filename; <br/> ifstream fin (filename. c_str (); <br/> If (! Fin) {<br/> cout <"the file cannot be opened! "<Endl; <br/> return 0; <br/>}< br/> // read data from a file <br/> char ch; <br/> char element [10]; <br/> int II = 0;/* counter */</P> <p> fin. get (CH); <br/> while (Ch! = '/N') {<br/> element [II ++] = CH; <br/> fin. get (CH); <br/>}< br/> element [II] = '/0'; <br/> Start = atoi (element ); <br/> II = 0; <br/> while (! Fin. EOF () {<br/> fin. Get (CH); <br/> while (Ch! = ''& Ch! = '/T' & Ch! = '/N') {<br/> element [II ++] = CH; <br/> fin. get (CH); <br/>}< br/> element [II] = '/0'; <br/> int e = atoi (element ); <br/> If (E! = 0) <br/> list. insertlast (E, list. length + 1); <br/> II = 0; <br/>}< br/> // start to simulate Joseph's Process <br/> node * begin = List. getnode (start); <br/> int stride = begin-> stride; <br/> node * Prev; <br/> while (list. length! = 0) {<br/> Prev = begin; <br/> for (INT I = 2; I <stride; I ++) {<br/> Prev = Prev-> next; <br/>}< br/> stride = Prev-> next-> stride; <br/> cout <Prev-> next-> Number <""; <br/> list. removenext (prev); <br/> begin = Prev-> next; <br/>}< br/> cout <Endl; <br/> getchar (); <br/> getchar (); <br/> return 0; <br/>}

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.