Huawei trial-elements sorted by parity (pyramid) and Huawei parity

Source: Internet
Author: User

Huawei trial-elements sorted by parity (pyramid) and Huawei parity

Question: elements are sorted by parity

Given an array input [], if the array length n is an odd number, place the largest element in the array in the middle of the output [] array,

If the array length n is an even number, place the largest element in the array to the right position in the middle of the output [] array,

Then, in the order of ascending to smallest, store the remaining number on both sides of the position where the largest element is placed in the order of one left and one right.


For example: input [] = {3, 6, 1, 9, 7} output [] = {3, 7, 9, 6, 1}; input [] = {3, 6, 1, 9, 7, 8} output [] = {1, 6, 8, 9, 7, 3}


#include <stdio.h>#include <stdlib.h>int compare(const void *p,const void *q){return *(int *)q - *(int *)p;}int main(){int i,j,a,k=0;int input[100];int output[100];scanf("%d",&a);    while(getchar()!='\n')      {          input[k++]=a;          scanf("%d",&a);     } input[k]=a;qsort(input,k+1,sizeof(int),compare);int f= (k+1)/2;output[f]=input[0];    for(i=1,j=1;i<=k;){output[f-j]=input[i];if((f+j)>k)break;output[f+j]=input[i+1];i+=2;j++;}for(i=0;i<=k;i++)printf("%d ",output[i]);printf("\n");return 0;}

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.