Title Description:
Enter a sequence of random numbers (each number in the sequence is an integer greater than 0, the length is known), and the initial count value M. Counting from the first digit of the sequence, counting to M, the number of the position is replaced by the count value m, and the number of the position is set out, and then counted from the next position from the new start until all the values of the series are out. If the count reaches the end of the series, the first position of the sequence is returned to count. Please program the above counting process, and output the order of the values in the Dequeue
Like what:
The input random sequence is: 3,1,2,4, the initial count value m=7, from the beginning of the sequence count (number 3 position)
The first round count column number is 2, the value is updated m=2, the number of columns is 3,1,4, from the position of the value 4 from the new start count
The second round count column is 3, the value is updated m=3, the number of columns is 1, 4, starting from the position of the value 1 is counted
The third round count column is 1, the value is updated m=1, the number of columns is 4, starting from the position of the value 4 is counted
The last round count is 4, and the counting process is completed.
The output value is in the order of: 2,3,1,4.
Required implementation functions:
void array_iterate (int len, int input_array[], int m, int output_array[])
"Input" int len: Enter the length of the sequence;
int intput_array[]: initial sequence of inputs
int m: initial count value
"Output" int output_array[]: output value out of order
"Back" none
Example:
Input: int input_array[] = {3,1,2,4},int len = 4, m=7
Output: output_array[] = {2,3,1,4}
Problem Solving Ideas:
Each time out a value, need to M, Input_array, Output_array, output position outpos, the starting position startpos to update;
For the output position Outpos calculation is the key! The analysis indicates that 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 position of the output M=input_array[outpos]; Startpos=outpos; printf ("Outpos is%d, M becomes%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 after each deletion of the sequence } } void Main () { int input_array[]={3,1,2,4}; int output_array[4]={0}; Array_iterate (4, Input_array, 7, output_array); printf ("dequeue order is \ n"); Print_array (4, Output_array);
Huawei Machine Test-Joseph Ring replacement counter M (array resolution)