【问题标题】:I want to output 1 4 2 3 5 odd number ascending order and even numbers in descending order我想输出 1 4 2 3 5 奇数升序和偶数降序
【发布时间】:2022-01-05 14:44:21
【问题描述】:

我想按升序输出奇数,按降序输出偶数我的代码在这里。

当我给输入n=5数组5 2 4 3 1,我想输出1 4 2 3 5, 但我得到了输出4 2 1 3 5。我不想要数组的位置。

#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int arr[n];

    for (int i = 0; i < n; ++i)
    {
        cin >> arr[i];
    }

// odd number ascending order
    for (int i = 0; i <= n; ++i)
    {
        for (int j = i + 1; j < n; ++j)
        {
            if (arr[i] < arr[j])
            {
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
    for (int i = 0; i < n; ++i)
    {
        if (arr[i] % 2 == 0)
        {
            cout << arr[i]<< " ";
        }
    }

// numbers in descending order
    for (int i = 0; i <= n - 1; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {

            if (arr[j] < arr[i])
            {
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
            }
        }
        if (arr[i] % 2 == 1)
        {
            cout << arr[i]<<" ";
        }
    }

    return 0;
}

【问题讨论】:

  • 我想按升序输出奇数,按降序输出偶数 -- std::partition 然后std::sort 两次。这基本上是一个 3 行 C++ 解决方案。其次,这个:cin &gt;&gt; n; int arr[n]; 不是有效的 C++。而是使用std::vector&lt;int&gt; arr(n);。顺便说一句——你是从那些提供可怕 C++ 示例的糟糕网站“学习”C++ 吗?
  • @PaulMcKenzie 这只是一个糟糕的建议。偶数和奇数的位置不能改变。
  • 可以给我孔代码,奇数按升序排列,偶数按降序排列
  • @karnanaskar "可以给我洞码升序奇数和降序偶数" 不!这不是 SO 的工作方式。请善待tour 到最后,并在此处发布问题之前通过help center 告知自己!
  • std::sortstd::partition 仍然可以工作,只是代码稍微多一点。

标签: c++ arrays sorting bubble-sort function-definition


【解决方案1】:

我们,初学者,应该互相帮助。:)

对于你我这样的初学者来说,这项任务并不容易。

我可以建议一种基于冒泡排序方法的方法。

你来了。

#include <iostream>
#include <utility>

template <typename UnaryPredicate>
void conditional_bubble_sort( int a[], size_t n, 
                              UnaryPredicate &&unary_predicate,
                              bool order = false )
{
    while ( n && !unary_predicate( *a ) )
    {
        ++a;
        --n;
    }
    
    for ( size_t i = 0, last = 0; !( n < 2 ); n = last )
    {
        last = 0;
        
        for ( size_t current = 0, next = 0; next < n; current = next )
        {
            while ( ++next < n && !unary_predicate( a[next] ) );
            
            if ( next != n )
            {
                if ( !order /* ascending */ ? a[next] < a[current] : a[current] < a[next] )
                {
                    std::swap( a[current], a[next] );
                    last = next;
                }
            }
        }
    }
}

int main() 
{
    int a[] = { 5, 2, 4, 3, 1 };
    const size_t N = sizeof( a ) / sizeof( *a );
    
    for ( const auto &item : a )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 != 0;} );
    
    for ( const auto &item : a )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    conditional_bubble_sort( a, N, []( const auto &item ) { return item % 2 == 0;}, true );
    
    for ( const auto &item : a )
    {
        std::cout << item << ' ';
    }
    std::cout << '\n';
    
    return 0;
}

程序输出是

5 2 4 3 1 
1 2 4 3 5 
1 4 2 3 5

【讨论】:

    【解决方案2】:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int n = 5;
        int left = 1, right = n - (n % 2);
        for(int i = 1; i <= n; i++)
        {
            if(i % 2 == 0)
            {
                cout << right << ' ';
                right-= 2;
            }
            else if(i % 2 == 1)
            {
                cout << left << ' ';
                left += 2;
            }
        }
        return 0;
    }
    
    

    【讨论】:

    • 但我不想改变位置,如果我输入 5 2 4 3 1 输出将是 1 4 2 3 5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 2022-12-13
    • 1970-01-01
    • 2022-11-13
    • 2021-05-30
    • 2020-09-20
    • 2017-06-24
    相关资源
    最近更新 更多