Huawei recruitment testing 7: an array of input [], Huawei input

Source: Internet
Author: User

Huawei recruitment testing 7: an array of input [], Huawei input
Huawei recruitment testing 7: an array of input []

Question: Give an array intput []
If the array length n is an odd number, place the largest element in the array in the middle of the output [] array. If the array length n is an even number, place the largest element in the array to the right of the two locations in the middle of the output [] array, and then place the elements in the order of size to size on both sides of the first position in sequence, store the remaining number in the order of one left and one right.
Example: input [] = {3, 6, 1, 9, 7} output [] = {3, 7, 9, 6, 1 };
Input [] = {3, 6, 1, 9, 7, 8} output [] = {1, 6, 8, 9, 7, 3}
Question Analysis:
This topic mainly deals with the arrangement of data between arrays and the transfer and receipt of direct data between arrays and arrays.
Algorithm ideas:
① First, sort the input integer data in ascending order. There is no need to use the Bubble Method to directly use sort in # include <algorithm>.
② Locate the middle position middle and put the largest data into middle
③ Next for loop,
1) WHEN n is an odd number, we can take the middle as the boundary directly, and go to the left to the right in a loop. I start from 1-> middle.
Because the left side is larger than the right side, the left side is larger than the right side.
2) When n is an even number, we take the middle as the boundary. Note that the first data in the output data cannot be cyclically reached, therefore, I can only start from 1-> middle-1, and put the smallest input value directly to the first place of the output array.
Here, we need to pay attention to a small trick: directly transmitting data from arrays. We 'd better set a new variable k.

========================================================== ========================================================
Reference code:

// Specify an array input [] // 2014.7.10 hepanhui # include <iostream> # include <algorithm> const int maxn = 100; using namespace std; void sort (int input [], int n, int output []) {int middle; int k = 1; sort (input, input + n); // if (n % 2) WHEN n is an odd number) {middle = (n-1)/2; output [middle] = input [n-1]; for (int I = 1; I <= middle; I ++) {output [middle-I] = input [n-1-k]; output [middle + I] = input [n-2-k]; k = k + 2 ;}} else {middle = n/2; output [middle] = input [n-1]; for (int I = 1; I <middle; I ++) {output [middle-I] = input [n-1-k]; output [middle + I] = input [n-2-k]; k = k + 2;} output [0] = input [0];} int main () {int n; cin> n; int input [maxn], output [maxn]; for (int I = 0; I <n; I ++) {cin> input [I];} sort (input, n, output ); for (int I = 0; I <n-1; I ++) {cout <output [I] <",";} cout <output [n-1] <endl; return 0 ;}

Mistakes in debugging:
① Here we do not need to # include <string> when entering an integer array, we generally tell the array size, we cannot use strlen to measure its size, because strlen is used to measure the string.
② When performing a for loop, note that I starts from 1, rather than 0, because the largest one in the middle has been assigned a value.
③ Data transmission between arrays. We sometimes use a new variable for convenience.




Given a two-dimensional array a, the array content should be reversed.

// This is acceptable.
# Include <stdio. h>
Void input (int * a, int row, int col );
Void transpose (int * p, int row );
Void output (int * a, int row, int col );
Void main ()
{
Int a [3] [3];
Input (a [0], 3 );
Transpose (a [0], 3 );
Output (a [0], 3, 3 );
}
Void input (int * a, int row, int col)
{
Int I;
For (I = 0; I <row * col; I ++)
Scanf ("% d", a + I); // It should be evicted *. The a + I here is already the address.
}
Void transpose (int * p, int row)
{
Int I, j, t;
For (I = 0; I <row; I ++)
For (j = I; j <row; j ++)
{// You have repeatedly asked the same element here, resulting in two conversions, so there is no conversion
// You only need to convert the number of triangles.
T = * (p + 3 * I + j );
* (P + 3 * I + j) = * (p + 3 * j + I );
* (P + 3 * j + I) = t;
}
}
Void output (int * a, int row, int col)
{
Int I;
For (I = 0; I <row * col; I ++)
{
If (I % row = 0) printf ("\ n ");
Printf ("% 6d", * (a + I ));
}
Printf ("\ n ");
}

How to find a number in a given Array

# Include <stdio. h>
Int main ()
{
Int n, a [20];
Int I, j, m;
While (scanf ("% d", & n )! = EOF)
{
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
}
Scanf ("% d", & m );
For (j = 0; j <n; j ++)
{
If (a [j] = m)
{
Printf ("% d \ n", j );
Break;
}
}
If (j = n) printf ("No \ n ");
}
Return 0;
}
Is the reason for multiple groups of tests
Note This line: while (scanf ("% d", & n )! = EOF)

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.