C++将数组重新排序为左边奇数右边偶数

2021-02-07 10:36:39 浏览数 (2)

参考链接: C 程序检查数字是偶数还是奇数

代码: 

#include <iostream>

#include <string.h>

using namespace std;

void L(int A[],int n) {

    int * B=new int [n] ();

    int * C=new int [n] (); 

int Q = 0, E = 0;

    for (int i = 0; i <= n; i ) {

        if (A[i] % 2 == 1) {

            B[Q] = A[i]; Q ;

        }

        else {C[E] = A[i]; E ;

    }

    }

    memcpy(A, B,Q*sizeof(int));

    memcpy(A Q, C,E*sizeof(int));

    delete[]B;delete [] C;

}

int main() {

    int A[9] = { 1,2,3,4,5,6,7,8,9 };

    L(A,9);

    for (int j = 0; j < 9; j ) {

        cout << A[j] << endl;

    }

}

运行结果:

0 人点赞