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;}