Home > Back-end >  Through the data structure problem of Joseph ring related thought
Through the data structure problem of Joseph ring related thought

Time:12-09

Joseph ring problem: set Numbers for 1, 2, 3,... N, n (n> 0) individuals sitting around a clockwise circle, m is any positive integer, starting from the first man since 1 clockwise order number off, report to dequeue stops and submitted to the m m, again from his next start to count off from 1, report to dequeue stops and submitted to the m m, so on, until all the complete dequeue, requirement, design a program simulating the process of any given m and n, and break the ranks number sequence,
Experimental requirements: sequence table is used to implement,

Contents:
1. Problem description
Joseph ring problem,


2. Data structure type definition
Typedef struct {
ElemType data [MaxSize];//store the elements in the data table
Int length;//store the length of the linear table
} SqList;


3. The module partition
(1) create a build order table function void CreateList (SqList * & amp; L, ElemType a [], int n)
(2) create linear output table function void DispList (SqList * L)
(3) create delete data element function bool ListDelete (SqList * & amp; L, int I ElemType & amp; E)
(4) create destruction of linear table function void DestroyList (SqList * & amp; L)
(5) main function void main ()


Design and analysis
4.To create an array in the main function, the array elements in the serial number of each person, and then to write number into the linear table, and then through the value of the input statement input value of n and m, and then through the circulation function to pick up the first m % n number of arrays, equivalent to delete the linear table of the elements, then linear rearrange the elements in the table, the equivalent of the element will be removed the next element as the beginning of the array, will rearrange the linear table, then the extracted elements have order into a new array, finally through the output statements to the new array output,

Main code
5.
 # include 
#include
# define MaxSize 50
Typedef int ElemType;
Typedef struct {
ElemType data [MaxSize];//store the elements in the data table
Int length;//store the length of the linear table
} SqList;//order sheet type
Void CreateList (SqList * & amp; L, ElemType a [], int n) {//build order table
Int I=0, k=0;
L=(SqList *) malloc (sizeof (SqList));
While (iL - & gt; Data=[k] a [I];
K++; i++;
}
L - & gt; Length=k;
}
Void DispList (SqList * L) {//output linear table (used to test linear element in the table is correct)
for(int i=0; iPrintf (" % d, L - & gt; Data [I]);
printf("\n");
}
Bool ListDelete (SqList * & amp; L, int I ElemType & amp; E) {//delete data element and the element extracted
int j;
Int k, num=0, p, q;
Int a, [50].
for(j=0; JA [j]=L - & gt; Data [j];
}
K=I % L - & gt; Length;
P=k;
Q=(I - 1) % L - & gt; Length;
E=L - & gt; Data [q];
for(j=1; J<=L - & gt; Length - 1. J++) {
L - & gt; The data/num=a, [p].
num++;
K++;
P=% k (L - & gt; Length);
}
L - & gt; Length -;
return true;
}
Void DestroyList (SqList * & amp; L) {//destruction of linear table
Free (L);
}
Int main () {
SqList * L;
ElemType a, [50].
Int n, I;
The scanf (" % d ", & amp; N);
for(i=1; i<=n; I++) {
A [I - 1]=I;
}
CreateList (* & amp; L, a, n);
Int m, e;
ElemType b [50];
The scanf (" % d ", & amp; M);
for(i=0; iListDelete (* & amp; L, m, e);
B [I]=e;
}
for(i=0; iPrintf (" % d, b [I]);
}
DestroyList (* & amp; L);
return 0;








  • Related