Huawei machine question-Joseph Ring

Source: Internet
Author: User
[Problem]

? Problem description:
Enter a series composed of random numbers (each number in the series is an integer greater than 0 and the length is known) and an initial count value m. Count from the first position of the series. After counting to M, replace the value at the position of the series with the Count value m, column the value at the position of the series, and start counting from the next position, until all values of the series are listed. If the Count reaches the end of the series, the system returns the first position of the series to continue counting. Please program the above counting process and output the order of numerical values in the column

For example, if the Input random number column is 3, 1, 2, and 4, the initial count value m = 7 starts from the first position of the series (where the value 3 is located)
In the first round, the number in the column is counted as 2, the Count value is updated to m = 2, and the columns are 3, 1, and 4 after the column is output. The count starts from the position where the value 4 is located.
In the second round, the number of columns to be counted is 3, the Count value is updated to M = 3, and the number of columns to be counted is, starting from the position where the value 1 is located.
In the third round of counting, the column number is 1, the Count value is updated to M = 1, and the column number is 4, starting from the position where the value 4 is located.
The counting process is complete when the number in the last round is 4.
Output values are listed in the order of 2, 3, and 4.

? Required implementation functions:
Void array_iterate (INT Len, int input_array [], int M, int output_array [])
[Input] int Len: length of the input series;
Int intput_array []: the initial input sequence.
Int M: initial count value
[Output] int output_array []: output value column Sequence
[Return] None

? Example
Input: int input_array [] = {3, 1, 2, 4}, int Len = 4, M = 7
Output: output_array [] = {2, 3, 4}

[Code]
# Include <stdio. h> # include <stdlib. h> # include <string. h> void array_iterate (INT Len, int input_array [], int M, int output_array []) {int Pos = 0, index; int I, K = 0; if (LEN <1) return; while (LEN> 0) {Index = (Pos + (M % Len) % Len; // out-of-column position Pos = index-1; // next start position if (Index = 0) {Index = Len; Pos = 0;} output_array [k ++] = input_array [index-1]; M = input_array [index-1]; for (I = index; I <Len; I ++) input_array [I-1] = input_array [I]; Len --;}} int main (void) {int input_array [] = {3, 1, 2, 4}; int output_array [4]; int Len = 4, M = 7; int I; array_iterate (Len, input_array, M, output_array); for (I = 0; I <Len; I ++) printf ("% d", output_array [I]); Return 0 ;}


Huawei machine question-Joseph Ring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.