Joseph ring problem single loop chain solution

xiaoxiao2021-03-30  209

Joseph ring problem

Time Limit: 1000ms Memory Limit: 65536ktotal Submit: 50 ACCEPTED: 8

Description

Joseph ring problem; N individuals enclose a ring, starting from the first person, reporting the M value exits the ring, and is replaced by his M value instead of the original M value, requiring the output of the left ring.

INPUT

There are 2 numbers, M and N in the first line. (0

OUTPUT

Outputs the order of the ring to exit the ring according to the format of the sample.

Sample Input

4 6

5 4 2 3 4 2

Sample Output

4, 1, 2, 3, 6, 5

Source

This is a universal solution, but for this question, there may be other algorithms, and the high people pass the guides!

#include

"

iostream

"

#include

"

CSTDIO

"

#include

"

CSTDLIB

"

Using

Namespace

std; typedef

Struct

cnode

...

{Int label; int data; struct cnode * next;}

CNODE;

int

M, N, I, Value, J; CNODE

*

p,

*

Q;

int

Main ()

...

{CIN >> M >> N; cnode * clist; q = (cnode *) malloc (sizeof (cnode)); clist = q; for (i = 1; i <= n; i ) ... {// CIN >> Value; cin >> Value; P = (cnode *) malloc (sizeof (cnode)); // p = new cnode; p-> label = i; p-> data = value; p-> next = NULL; Q-> Next = p; q = p; if (i == n) q-> next = clist-> next;} p = clist; // cout << p-> label << endl; for i = 1; i <= n; i ) ... {for (j = 1; j next;} q = p-> next; m = Q- > data; if (i == n) ... {cout << q-> label << Endl; break;} cout << q-> label << ","; p-> next = Q-> Next Delete q;} // system ("pause"); return 1;}

Array solving method: Source #include

<

iostream

>

Using

Namespace

STD;

int

Main ()

...

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

New Post(0)