Interview 14: Adjust the array order so that the odd digits are preceded by even numbers

Source: Internet
Author: User

//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

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.