Josephus Joseph

xiaoxiao2021-03-06  22

Solving Josephus Problem: The N numbers are set up. From the number of counts from the count, the number of M is popped up, then the number of numbers from the next number, count to M The number is also popped up, so repeated until all the numbers are popped up. Outputs the sequence of these numbers.

#include #include #define maxSize 100typedEf struct node {int No; int m; struct node * next;} node, * linklist; linklist creat (int M, int N, int mm [ ]) {LinkList L; INT I; Node * R, * S; L = (LinkList) Malloc (SizeOf (Node)); R = L; For (i = 0; i NO = i 1; s-> m = mm [i]; r-> next = S; r = s;} l-> no = 0; L-> m = 0; r-> next = L; return L;} void Josephus (Linklist L, Int M, INT n) {INT I, J, NN = N; Node * p, * q; p = L; for (i = 0; i no == 0) P = p-> next; p = p-> next;} q = p-> next; if (q- > no == 0) q = q-> NEXT; M = Q-> m; Printf ("% 4d", q-> no); p-> next = q-> next; free (q); nn- -; p = p-> next;}} main () {Int m, n, i, mm [maxSize] / * = {3, 1, 7, 2, 4, 8, 4} * /; int CH = 0, C; LinkList L; / * m = 20; n = 7; Only for Test * / Printf ("Input M:"); Scanf ("% D", & M); Printf ("Enter N:"); Scanf ("% D", & n); Printf ("Enter a password:"); for (i = 0; i

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

New Post(0)