Joseph Ring The problem is from the number of people began to count K, the number to m of the person out; his next man is from 1.
The number of people who count to M goes out again, counting the number of the last person left.
Below are two methods, sequential table implementation and linked list implementation .
The code for the linked list:
typedef struct LINKNODE{DATATYPE data;struct Linknode *next;} linknode,*plinknode;typedef struct Linklist{linknode *phead;} Linklist,*plinklist;plinknode josephcircle (plinklist plist,int k,int m)//Count from K, count to M dequeue {assert (pList); if (NULL = = Plist->phead) {printf ("empty list \ n"); return NULL;} Plinknode cur = Plist->phead;plinknode del = null;while (--k) {cur = cur->next;} while (cur->next! = cur) {int tmp = M;while (--tmp) {cur = cur->next;} printf ("The element to be removed is%d\n", cur->data);d el = cur->next;cur->data = Cur->next->data;cur->next = cur-> Next->next;free (DEL);} return cur;}
Sequential Table code:
#define _cre_secure_no_warnings 1#include<stdio.h> #include <malloc.h>void joscircle (int n, int k, int m) { int *p = (int *) malloc (n*sizeof (int)); int i = 0;int out = 0;int count = 0;//is used to count when Count reaches M, the number of people is out of order (i = 0;i < n;i++ {* (p + i) = 1;//All people are assigned a value of 1, and if someone is out of the queue, change its value to 0}i = K-1;//while (out! = n-1) {if (p[i] = = 1)//If the person is in a position of 1, indicating that this person is still in the team, counting can include him Count++;if (count = = m)//if counted to a person, count is exactly M, at this point, the person is out of the row, the number corresponding to 0{p[i] = 0;count = 0;out++;//The number of people plus 1}i++;if (i = = N)// Because of a total of n individuals, the array subscript i from 0 to n-1, if the number to N, we will set it to 0{i = 0;}} for (i = 0;i < n;i++) {if (P[i])//The person whose number is not changed to 0 is the person who eventually does not have a dequeue {printf ("%d person eventually does not dequeue", i+1);}} Free (p);} int main () {joscircle (10,2,3);//10 persons in a circle, starting from the second person counting, counting to 3 of the person to dequeue system ("pause"); return 0;}
Joseph Ring Question