利用递归求数组的逆序
#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; }