//face question 14_ Adjust the array order so that the odd digits precede the even number. cpp: The entry point that defines the console application. //#include"stdafx.h"#include<iostream>using namespacestd;voidSwapint*begin,int*end) { inttemp; Temp=*begin; *begin=*end; *end=temp;}voidReorderoddeven (int*pdata,unsignedintlength) { if(pdata==null| | length==0) return; int*begin=pdata,*end= (pdata+length-1); while(begin<end) { while((*begin)%2==1) Begin++; while((*end)%2==0) End--; if(begin<end) Swap (begin,end); }}int_tmain (intARGC, _tchar*argv[]) { intpdata[]={1,2,3,4,5,6,7,8,9,Ten}; Reorderoddeven (PData,sizeof(PData)/sizeof(int)); for(intI=0;i<sizeof(PData)/sizeof(int); i++) {cout<<pData[i]<<" "; } cout<<Endl; return 0;}
Idea: 1, begin points to the 1th position, end points to the last position;
2, begin to find the first is even, end find the first is an odd position;
3. Exchange
4, if begin>end, repeat 2;
Interview 14: Adjust the array order so that the odd digits are preceded by even numbers