把数组中的奇数放到偶数之前

案例

数组内容:3 4 4 6 8 2 1 1 1

调换奇偶:3 1 1 1 8 2 4 4 6

 

思路

源于快速排序

方式1

参考代码

复制代码
#include <iostream>
#include <cstring>
using namespace std;

bool IsOdd(int num)
{
    return num % 2 == 1 ? true : false;
}
bool changeArray(int *a, int size)
{
    if(size <= 0)
        return false;
    int oddPartition = -1;
    int cur = 0;
    for(; cur < size; ++cur)
    {
        if(IsOdd(a[cur]))
        {
            if(oddPartition + 1 != cur)
            {
                int tmp = a[oddPartition+1];
                a[oddPartition]= a[cur];
                a[cur] = tmp;
            }
            ++oddPartition;
        }
    }
    return true;
}


int main()
{
    int a[] = {3, 4, 4, 6, 8, 2, 1, 1, 1};
    int size = sizeof(a) / sizeof(int);

    for(int i = 0; i < size; ++i)
        cout << a[i] << " ";
    cout << endl;

    changeArray(a, size);
    for(int i = 0; i < size; ++i)
        cout << a[i] << " ";
    cout << endl;
}
    
复制代码

 

方式2

参考代码

复制代码
#include <iostream>
#include <cstring>
using namespace std;

bool IsOdd(int num)
{
    return num % 2 == 1 ? true : false;
}
bool changeArray(int *a, int size)
{
    if(size <= 0)
        return false;
    int beg = 0, end = size -1;
    while(beg < end)
    {
        while(beg < end && IsOdd(a[beg]))
            ++beg;
        while(beg < end && !IsOdd(a[end]))
            --end;
        if(beg < end)
        {
            int tmp = a[beg];
            a[beg] = a[end];
            a[end] = tmp;
        }
    }
    return true;
}


int main()
{
    int a[] = {3, 4, 4, 6, 8, 2, 1, 1, 1};
    int size = sizeof(a) / sizeof(int);

    for(int i = 0; i < size; ++i)
        cout << a[i] << " ";
    cout << endl;

    changeArray(a, size);
    for(int i = 0; i < size; ++i)
        cout << a[i] << " ";
    cout << endl;
}
    
复制代码

扩展

不是奇偶问题,别掉条件

比如正负,需要把IsOdd()函数换成判断正负的函数;

比如被5整除,需要把IsOdd()函数换成判断被5整除的函数;

。。。。。。

这是一类问题,可以给出一个模式来解决。如下

复制代码
#include <iostream>
using namespace std;

bool isEven(int val)
{
    return ((val & 0x1) == 0) ? true : false;
}
void ReOrder(int *a, int size, bool (*func)(int))
{
    if (a == NULL || size <= 0)
        return;
    int beg = 0, end = size-1;
    while (beg < end)
    {
        while (beg < end && !func(a[beg]))
            ++beg;
        while (beg < end && func(a[end]))
            --end;
        if (beg < end)
            swap(a[beg], a[end]);
    }
}
void ReOrderOddEven(int *a, int size)
{
    ReOrder(a, size, isEven);
}
    
void tranverse(int *a, int size)
{
    if (a == NULL || size <= 0)
        return;
    for (int i = 0; i < size; ++i)
        cout << a[i] << " ";
    cout << endl;
}
int main()
{
    int a[] = {3, 2, 5, 2, 8, 3, 3, 0, 9};
    int size = sizeof(a) / sizeof(int);
    tranverse(a, size);
    ReOrderOddEven(a, size);
    tranverse(a, size);
}
复制代码

 




本文转自jihite博客园博客,原文链接:http://www.cnblogs.com/kaituorensheng/p/3607118.html,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值