Topic 1516: Adjusting the array order so that the odd digits are preceded by even numbers
time limit:1 seconds
Memory limit:128 MB
Special question: No
submitted:3416
Resolution:1091
-
Title Description:
-
Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits are placed in the first half of the array, all the even digits are located in the second half of the array, and the relative positions between the odd and odd, even and even, are guaranteed.
-
Input:
-
Each input file contains a set of test cases.
For each test case, enter an n for the first row, representing the number of digits in the array.
Enter n integers for the next line. Represents the number of n in an array.
-
Output:
-
corresponding to each test case,
Enter a row of n digits, representing the adjusted array. Note that the numbers and numbers are separated by a space, and there are no spaces after the last number.
-
Sample input:
-
51 2 3) 4 5
-
-
Sample output:
-
1 3 5) 2 4
1#include <cstdio>2#include <vector>3 using namespacestd;4 5 intMain () {6vector<int>V, V_even, v_odd;7 intN, temp, size, I;8 while(SCANF ("%d", &n)! =EOF) {9 for(i =0; I < n; i++) {Tenscanf"%d", &temp); One if(Temp%2==1) A V_even.push_back (temp); - Else - V_odd.push_back (temp); the } -Size =v_even.size (); - if(Size! =0) -printf"%d", v_even[0]); + for(i =1; i < size; i++) -printf"%d", V_even[i]); +Size =v_odd.size (); A for(i =0; i < size; i++) atprintf"%d", V_odd[i]); -printf"\ n"); - } - return 0; -}
The simplest method is to use two containers to install them. Another way is to merge the sort, thinking about the same, in the merger is the first two arrays of the odd number first put in, and then put the even in.
Nine degrees OJ topic 1516: Adjust the array order so that the odd digits are preceded by even numbers