C + + in the Joseph ring replacement counter m of array solution

  • 2020-05-19 05:15:15
  • OfStack

C + + Joseph ring replacement counter m (array resolution)

Title description:

Enter a sequence composed of random Numbers (each number in the sequence is an integer greater than 0 with a known length), and the initial count value m. Start counting from the position at the beginning of the sequence. After counting to m, replace the value at the position of the sequence to m, and then count the value at the position of the sequence to m. Then count again from the position of 1 below until all the values in the sequence are listed. If the count reaches the end of the sequence, the count continues at the beginning of the sequence. Please programmatically implement the above counting process and output the numerical values in the order in which they are listed

Such as:

The input random number is listed as: 3,1,2,4, and the initial count value m=7. Count from the position at the beginning of the sequence (the position at which the number 3 is located).
In the first round, the count out number is 2, the count value is updated m=2, the count out number is listed as 3,1,4, and the count starts from the position of 4
In the second round, the count out number is 3, the count value is updated m=3, the number after the count out is listed as 1,4, and the count starts from the position where the value 1 is
In the third round, the counting out number is 1, the counting value updates m=1, and the number after the counting out is listed as 4, counting from the position of 4

The last round of counting out the number is 4, the counting process is completed.

The output values are listed in the order of 2,3,1,4.

Required to implement functions:


void array_iterate(int len, int input_array[], int m, int output_array[])

int len: length of input sequence;
int intput_array[] : the initial sequence of input
int m: initial count

[output] int output_array[] : the sequence of output values

[return] none

Example:

Input: int input_array[] = {3,1,2,4}, int len = 4, m=7
Output: output_array[] = {2,3,1,4}

Solution idea:

Each time one value is listed, m, input_array, output_array, output location outPos and starting location startPos need to be updated;

The calculation of the output location outPos is key! According to the analysis, outPos=(startPos+ m-1)%num


#include <stdio.h> 
 
void print_array(int len, int array[])  
{  
  for(int i=0; i<len; i++)  
    printf("%d ", array[i]);  
  printf("\n");  
}  
  
void array_iterate(int len, int input_array[], int m, int output_array[])  
{  
  int startPos=0;  
  int outPos;  
  int nIter=len-1;  
  int num=len;  
  for(; nIter>=0; nIter--)  
  {  
    outPos=(m+startPos-1)%num;// Difficulty: calculate the output position   
    m=input_array[outPos];  
    startPos=outPos;  
    printf("outPos is %d, m into  %d\n", outPos, m);  
    output_array[len-nIter-1]=m;  
    for(int i=outPos; i<num-1; i++)  
      input_array[i]=input_array[i+1];  
    num--;  
    print_array(num, input_array); // Output the sequence after each deletion   
  }  
}  
void main()  
{  
  int input_array[]={3,1,2,4};  
  int output_array[4]={0};  
  array_iterate(4, input_array, 7, output_array);  
  printf(" The order of going out is \n"); 
  print_array(4, output_array); // The final output output_array 
}  

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: