#include  <iostream>
using namespace std;

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void reverse(int *A, int low, int high)
{
    if (low < high)
    {
        //int temp = A[low];  //交换两个数 类数组形式
        //A[low] = A[high];
        //A[high] = temp;
        //int temp = *(A + low);  //交换两个数 指针形式
        //*(A + low) = *(A + high);
        //*(A + high) = temp;
        swap((A + low), (A + high));  //交换两个数 函数形式
        reverse(A, low + 1, high - 1); //递归调用 
    }
}
int main()
{
    int arr[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    reverse(arr, 0, 9);
    for (int i = 0; i < 10; i++)
    {
        cout << arr[i] << ' ';
    }
    cout << endl;
    return 0;
}

利用递归求数组的逆序

 

相关文章:

  • 2021-05-04
  • 2022-02-19
  • 2021-12-10
  • 2021-06-03
  • 2022-12-23
猜你喜欢
  • 2021-12-10
  • 2022-12-23
  • 2021-10-01
  • 2022-12-23
  • 2021-09-20
  • 2022-02-17
  • 2021-07-28
相关资源
相似解决方案