Huawei machine Questions-array sorting with odd odd positions, even in even locations

Source: Internet
Author: User

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

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.