The topic is very simple, is to give you an array, sort it, and after sorting, the odd number to be placed in the odd position, even in the position of an even number, if this rule is not satisfied, the array is filled with 0
Implementation of the code as follows, it is worth noting how to interpret this number is odd or even, I would like to use bit operation is the fastest way.
//test_huawei.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include<iostream>#include<string.h>using namespacestd;#defineT 10int_tmain (intARGC, _tchar*argv[]) { //implement an odd number in an odd-numbered position even if it is not added 0 inta[t]={1,2, at,1, at, +, About,9, +, -}; intb[2*t]={0};//Consider the worst case new array should be twice times the size of the original array (strictly speaking 2t-1) inti,j,temp; for(i=0; i<t;i++) { for(j=i+1; j<t;j++) { if(a[i]>A[j]) {Temp=A[i]; A[i]=A[j]; A[J]=temp;//Bubble Sort} }} J=0; for(i=0; i<t;i++) {cout<<a[i]<<"\ t"; if(a[i]&1)//Odd//use bit and operation to reduce the amount of computation { if((j+1) &1)//Odd Positionb[j]=A[i]; Else{B[j]=0; B[j+1]=A[i]; J++; } } Else //even { if((j+1) &1) {B[j]=0; B[j+1]=A[i]; J++; } ElseB[j]=A[i]; } J++; } cout<<Endl; for(i=0;i<2*t;i++) {cout<<b[i]<<" "; } cout<<Endl; return 0;}
Execution effect:
Huawei machine Questions-array sorting with odd odd positions, even in even locations