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

Source: Internet
Author: User
Tags printf first row

Title Description:

Enter an array of integers to implement a function that adjusts the order of the numbers in the array so that all the odd digits are placed in the first half of the array, all the even digits are located in the second half of the array, and the relative positions between the odd and odd, even and even, are guaranteed.

Input:

Each input file contains a set of test cases.
For each test case, enter an n for the first row, representing the number of digits in the array.
Enter n integers for the next line. Represents the number of n in an array.

Output:

corresponding to each test case,
Enter a row of n digits, representing the adjusted array. Note that the numbers and numbers are separated by a space, and there are no spaces after the last number.

Sample Input:

5
1 2 3 4 5

Sample output:

1 3 5) 2 4

AC Code:


#include <stdio.h> #include <stdlib.h> int main () {int i, n;
        while (scanf ("%d", &n)! = EOF) {int *a = (int *) malloc (sizeof (int) *n);
        int *odd = (int *) malloc (sizeof (int) *n);
        int *even = (int *) malloc (sizeof (int) *n);
        int oindex = 0, eindex = 0;
            for (i = 0; i < n; i++) {scanf ("%d", &a[i]);   
            If (A[i]% 2) {odd[oindex++] = A[i];
            } else {even[eindex++] = A[i];
            }} for (i = 0; i < Oindex; i++) {if (i! = n-1) {printf ("%d", odd[i]);
            } else {printf ("%d", odd[i]); }} for (i = 0; i < Eindex; i++) {if (i! = eIndex-1) {printf ("%d", Even[i]
            );
            } else {printf ("%d", even[i]);
 
        }} printf ("\ n");
        Free (a); A =NULL;
        Free (odd);
        Odd = NULL;
        Free (even);
    even = NULL;
} return 0;
    }/************************************************************** problem:1516 User:wusuopubupt language:c
 result:accepted time:70 Ms memory:2088 KB ****************************************************************/




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.