Split Parity Funktsiyani tartibga soluvchi bahslar

Men bunga qarshiman va nima qilayotganimni bilolmayman. Men ketma-ketlikning oldida bo'lishni g'alati tarzda ajratadigan funksiyani yozmoqchiman. [3, 1, 4, 2] yoki [1, 3, 2, 4] ikkala maqbuldir.

Hozirda int arr [5] = {3,6,4,1,12} ni ishga tushiradigan bo'lsam, men 3,4,1,6,2 chiqishni boshlayman. noto'g'ri ish.

Quyidagi kod:

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;
        }
    }
}
1
qo'shib qo'ydi muallif chema989, manba

6 javoblar

To'g'ri xususiy taqqoslash funktsiyasi bilan standart algoritmlardan foydalaning:

#include 
#include 
#include 

template
bool is_odd(const T& value)
{
    if (value & 1)
        return true;
    return false;
}

struct odd_first
{
    template
    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(std::cout, ", "));
    std::cout << std::endl;
}

kutilgan natijalar:

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

Fezvedning taklifiga ko'ra, std :: division:

#include 
#include 
#include 

struct is_odd
{
    template
    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(std::cout, ", "));
    std::cout << std::endl;
}
0
qo'shib qo'ydi

To'g'ri xususiy taqqoslash funktsiyasi bilan standart algoritmlardan foydalaning:

#include 
#include 
#include 

template
bool is_odd(const T& value)
{
    if (value & 1)
        return true;
    return false;
}

struct odd_first
{
    template
    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(std::cout, ", "));
    std::cout << std::endl;
}

kutilgan natijalar:

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

Fezvedning taklifiga ko'ra, std :: division:

#include 
#include 
#include 

struct is_odd
{
    template
    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(std::cout, ", "));
    std::cout << std::endl;
}
0
qo'shib qo'ydi

You can do this much, much easier with std::sort: Demo

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

Chiqish:

3 1 6 4 12 

Or, if you desire internal sorting within odd and even: 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);
    });

Chiqish:

1 3 4 6 12 
0
qo'shib qo'ydi

You can do this much, much easier with std::sort: Demo

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

Chiqish:

3 1 6 4 12 

Or, if you desire internal sorting within odd and even: 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);
    });

Chiqish:

1 3 4 6 12 
0
qo'shib qo'ydi

Hamma narsani o'zgartirgandan keyin yana bir marta indexCounter ga qarashingiz kerak edi (agar siz kodni birinchi marta takrorlashda kelgan qiymatni bilmasangiz arr [indexCounter] = = arr [indexCounter + 1]; to'g'ri yoki noto'g'ri)

Simplest fix is to add indexCounter --; just after arr[arrSize - 1] = tempValueHolder;

Bundan tashqari, juda yaxshi emas va men std :: qismiga qarashni tavsiya qilaman

0
qo'shib qo'ydi

Hamma narsani o'zgartirgandan keyin yana bir marta indexCounter ga qarashingiz kerak edi (agar siz kodni birinchi marta takrorlashda kelgan qiymatni bilmasangiz arr [indexCounter] = = arr [indexCounter + 1]; to'g'ri yoki noto'g'ri)

Simplest fix is to add indexCounter --; just after arr[arrSize - 1] = tempValueHolder;

Bundan tashqari, juda yaxshi emas va men std :: qismiga qarashni tavsiya qilaman

0
qo'shib qo'ydi