The "algorithm" adjusts the order of the array so that the odd and even positions are unchanged after the first even number.

Source: Internet
Author: User

Title: Enter an array of shapes, reorder the arrays so that all the odd numbers are in the front even, and the relative positions between the odd and the even are not changed.

Thought: Iterates through all arrays starting at the beginning of the array. When an even number is encountered, the even is packaged, that is, the count of the even numbers so far, and these even as a whole, and when an odd number is encountered, the odd point is swapped with the previous even integer.

#include  <stdio.h> #include  <stdlib.h>void nuo (INT&NBSP;*A,INT&NBSP;J,INT&NBSP;NU)                       //the odd and all the even swapped positions in front {        int b=a[j];         while (nu--) {                 a[j]=a[j-1];                 j--;                    }        a[j]=b;} Int main () {        int* a;         int i = 0 ;        int j  = 0 ;        int nu= 0 ;         printf ("Enter the number of arrays: \ n"), scanf ("%d", &i), A=malloc (sizeof (int) *i);p rintf ("Please enter an array separated by commas: \ n"); for (; j<i;j++) { scanf ("%d,", &a[j]);} J=0;        while (I!=J) {                 if (a[j]%2==1) {                    //This number is odd                           nuo (A,J,NU);                         j++;                               }                 else{                             //When this number is an even number, record even numbers                           j++;                         nu+=1;                     }            }        printf ("a[%d]=", I);         j=0;        while (i!=j ) {&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;   printf ("   %d", a[j++]);                    }         printf ("\ n");         return 0;}


This article is from the "Ling Feng 2019" blog, be sure to keep this source http://lingfeng2019.blog.51cto.com/12741667/1910061

The "algorithm" adjusts the order of the array so that the odd and even positions are unchanged after the first even number.

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.