数组颠倒算法

#include <iostream>
#include <iterator>
using namespace std;

void reverse(int* A, int lo, int hi)
{
    if (lo < hi)
    {
        swap(A[lo], A[hi]);
        reverse(A, ++lo, --hi);
    }
}

void reverse(int* A,int n)
{
    reverse(A, 0, n-1);
}
int main()
{
    int A[6] = { 1, 2,3,4,5,6 };
    reverse(A,6);

    std::copy(std::begin(A), std::end(A), std::ostream_iterator<int>(std::cout, "\n"));
}

输出6,5,4,3,2,1

posted on 2019-08-15 13:57  bluesky0318  阅读(170)  评论(0编辑  收藏  举报