Title: Enter an array of integers to implement a function to adjust the numbers in the array so that all the odd digits are in the first half of the array, and all the even digits are in the second half of the array.
Code:
1#include <stdio.h>2 BOOLIsEven (intN)3 {4 return(N &1) ==0;5 }6 voidReorder (int*pdata, unsignedintLengthBOOL(*func) (int))7 {8 if(PData = = NULL | | length = =0)9 return;Ten int*pbegin =PData; One int*pend = pData + Length-1; A while(Pbegin <pEnd) - { - while(Pbegin < pEnd &&!func (*pbegin))//If the previous pointer points to a number that is odd thepbegin++; - while(Pbegin < PEnd && func (*pend))//If the number pointed to by the following pointer is even -pend--; - if(Pbegin <pEnd) + { - inttemp = *Pbegin; +*pbegin = *pEnd; A*pend =temp; at } - } - - } - intMain () - { in inta[8] = {5,1,6,2,3,7,8,Ten }; -Reorder (A,8, IsEven); to for(inti =0; I <8; i++) +printf"%3d", A[i]); -printf"\ n"); the return 0; * } $ /*Panax Notoginseng 5 1 7 3 2 6 8 - Please press any key to continue ... the + */
View Code
Adjust the array order so that the odd number is preceded by even numbers--the sword refers to an offer