【问题标题】:Split Parity Function ordering odds and evens拆分奇偶校验函数排序奇数和偶数
【发布时间】:2017-03-02 16:39:18
【问题描述】:

我一直盯着这个,我不知道我做错了什么。我正在尝试编写一个函数,将奇数重新排序到数组中偶数的前面。内部奇偶顺序并不重要,意思是[3, 1, 4, 2] 或[1, 3, 2, 4] 都可以接受。

目前,如果我初始化 int arr[5] = {3,6,4,1,12},我会得到 3,4,1,6,2 的输出 努力找出我做错了什么。

代码如下:

void SplitParity(int arr[], int arrSize)
{
    int tempValueHolder;

    for (int indexCounter = 0; indexCounter < arrSize; indexCounter++)
    {
        //Iterate through each index checking for odd
        if (arr[indexCounter] % 2 == 0)
        {
            tempValueHolder = arr[indexCounter];

            //If Odd.....shift all indexes forward and move current to the back
            for (int innerCounter = indexCounter; innerCounter < arrSize; innerCounter++)
                arr[innerCounter] = arr[innerCounter + 1];

            arr[arrSize - 1] = tempValueHolder;
        }
    }
}

【问题讨论】:

标签: c++ arrays sorting


【解决方案1】:

你忘记了,在转换完所有内容后,你必须再次查看indexCounter(你不知道在你做arr[indexCounter] = arr[indexCounter+ 1];时循环的第一次迭代中到达的值是否正确)

最简单的解决方法是在 arr[arrSize - 1] = tempValueHolder; 之后添加 indexCounter --;

它也非常低效,我建议你看看 std::partition

【讨论】:

    【解决方案2】:

    使用具有正确自定义比较功能的标准算法:

    #include <iterator>
    #include <iostream>
    #include <algorithm>
    
    template<class T>
    bool is_odd(const T& value)
    {
        if (value & 1)
            return true;
        return false;
    }
    
    struct odd_first
    {
        template<class T>
        bool operator()(const T& l, const T& r) const {
            if (is_odd(l))
            {
                if (is_odd(r))
                    return false;
                return true;
            }
            else
                return false;
        }
    };
    
    
    
    int main()
    {
        int vals[] = { 5, 6, 7, 8, 9, 1, 2, 3, 4, 0 };
    
        std::sort(std::begin(vals), std::end(vals), odd_first());
        std::copy(std::begin(vals), std::end(vals), std::ostream_iterator<int>(std::cout, ", "));
        std::cout << std::endl;
    }
    

    预期输出:

    5, 7, 9, 1, 3, 6, 8, 2, 4, 0, 
    

    按照 Fezvez 的建议,std::partition:

    #include <iterator>
    #include <iostream>
    #include <algorithm>
    
    struct is_odd
    {
        template<class T>
        bool operator()(const T& value) const
        {
            if (value & 1)
                return true;
            return false;
        }
    };
    
    int main()
    {
        int vals[] = { 5, 6, 7, 8, 9, 1, 2, 3, 4, 0 };
    
        std::partition(std::begin(vals), std::end(vals), is_odd());
        std::copy(std::begin(vals), std::end(vals), std::ostream_iterator<int>(std::cout, ", "));
        std::cout << std::endl;
    }
    

    【讨论】:

      【解决方案3】:

      使用std::sort,您可以轻松得多:Demo

      std::sort(std::begin(arr), std::end(arr), [](int lhs, int rhs){
          return (lhs % 2 == 1 && rhs % 2 == 0);
      });
      

      输出:

      3 1 6 4 12 
      

      或者,如果您希望在奇数和偶数内进行内部排序:Demo

      std::sort(std::begin(arr), std::end(arr), [](int lhs, int rhs){
              int l_res = lhs % 2;
              int r_res = rhs % 2;
              if (l_res == r_res)
                  return lhs < rhs;
              return (l_res == 1 && r_res == 0);
          });
      

      输出:

      1 3 4 6 12 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-04
        • 2013-03-05
        • 2015-05-31
        • 2015-06-29
        • 1970-01-01
        • 2014-03-06
        • 1970-01-01
        相关资源
        最近更新 更多