1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. // 数组元素逆置
    5. // 1.创建数组
    6. int arr[5] = {1, 3, 2, 5, 4};
    7. cout << "数组逆置前的数组" << endl;
    8. for (int i = 0; i < 5; i++) {
    9. cout << arr[i] << endl;
    10. }
    11. // 2.实现逆置
    12. // 2.1记录起始下标位置
    13. // 2.2 结束下标位置
    14. // 2.3 起始位置与结束下标位置互换
    15. // 2.4 起始位置++ 结束位置 --
    16. // 2.5 循环执行2.1操作,知道起始位置>=结束位置
    17. int start = 0;
    18. int end = sizeof(arr) / sizeof(arr[0]) - 1;
    19. while (start < end) {
    20. int temp = arr[start];
    21. arr[start] = arr[end];
    22. arr[end] = temp;
    23. start++;
    24. end--;
    25. }
    26. // 3.打印逆置后的数组
    27. cout << "数组元素逆置后" << endl;
    28. for (int i = 0; i < 5; i++) {
    29. cout << arr[i] << endl;
    30. }
    31. return 0;
    32. }