From the structure-oriented object ----- Josephus expansion problem (1)

xiaoxiao2021-03-06  40

Today, I saw Josephus questions, suddenly I want to write some things, combined with my own thoughts, so I wrote these posts. Because there are a few code a bit long, I will be developed. If you have any help you have, I am gratifying. Maybe you will say that there is a lot of books in this question, but I am writing in different ways, so that beginners will experience the difference between the facing-oriented object; and you can also see what I wrote. And the differences in some books. If you are a prawn, you can smile, or enlighten me.

Josephus expansion problem (1): A group of children enclose a circle, and the M numbers must be counted in a clockwise direction. From the first child, ask for one and a few winners.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//ring.h

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Struct Node

{

Int code;

Node * Next;

}

Class Ring

{

PUBLIC:

Ring (INT N);

~ Ring ();

Void Progress (INT M);

void printnode ();

Void Clearcode ();

Void Display ();

protected:

Node * HEAD;

Node * pivot;

Node * pcur;

}

/ / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /Ring.cpp//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ # include #include #include "ring.h"

Ring :: rG (int N) {head = new node [n]; pcur = head;

For (int i = 1; i <= n; i , pcur = pcur-> next) {pcur -> code = i; pcur -> next = head (i% n); // printnode (); // If you want to look the boy who was you infut, you can live out "//"}

Display (); pcur = & head [n-1];

Ring :: ~ Ring () {delete [] head;}

Void Ring :: Progress (int = 0; i next;}}

Void Ring :: PrintNode () {static int linecount;

IF ((LINECount )% 10) == 0) cout << Endl; cout << setw (4) << pcur -> code;

Void ring :: clearcode () {pivot-> next = pcur-> next; pcur = pivot;

}

Void Ring :: Display () {node * pb = pcur; do {printnode (); pcur = pcur-> next;} while (pb! = pcur); cout << endl;}

/ / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ / Jose.h// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ Class Jose {public: Jose (int BOYS = 10, int based = 1, int m = 3); void initial (); void getwinner (); protected: int number; int started: int name; int }

/ / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ / Jose.cpp//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~ # include #include "ring.h" #include "jose.h"

Jose :: Jose (int BOYS, INT BEGIN, INT M) {IF (Boys <1) {cout << "Bad Number of Boys! / N"; Return;

IF (Begin <0) {cout << "Bad beginning position! / n"; return;}

IF ((M> BOYS)) {cout << "Bad Interval number! / n"; return;}

Numofboys = BOYS; beginpos = begin; interval = m;

Void Jose :: getwinner () {Ring x (Numofboys); X.Progress (Beginpos);

For (int i = 1; i

Void Jose :: Initial () {Int Num, Begin, M, W; Cout << "please input the number of boys:" << endl << "beginning position, / n innterbal per count" << Endl << " Number of Winners: "<< endl; cin >> Num >> begin >> m >> W;

IF (Num <2) {CERR << "Bad Number Of Boys" << Endl; Return;} IF (Begin <0) {CERR << "BAD BEGINNING POSITION." "

IF (M <1 || M> Num) {CERR << "BDA Interval Number." << endl;}

IF (W <1 || W> = Num) {CERR << "Bad Number of Winners."; Return;}

Numofboys = NUM; beginpos = begin; interval = m; WINS = W;}

/ / ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ / main.cpp// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~

#include "jose.h"

Void main () {jose.initial (); jose.getwinner ();

转载请注明原文地址:https://www.9cbs.com/read-71349.html

New Post(0)