c++程序—逆置

#include<iostream>
using namespace std;
#include<string>
#include<ctime>

int main()
{
    //数组元素的逆置
    int arr[5] = { 1,5,3,2,4 };
    int start = 0;
    int end = sizeof(arr) / sizeof(arr[0]) - 1;

    cout << "逆置前的数组为:" << endl;
    for (int i = 0; i <= end; i++)
    {
        cout << arr[i] << "\t";
    }
    cout << endl;

    for (; start < end; start++, end--)
    {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
    }

    for (int i = 0; i < 5; i++)
    {
        cout << arr[i] << "\t" ;
    }
    cout << endl;

    //for (int i = 0; i < 5; i++)
    //{
    //    cout << arr[i] << "\t" ;
    //}
    //cout << endl;
    //int temp=0;
    //for (int i = 0; i < (5 - 1) / 2; i++)
    //{
    //    temp = arr[i];
    //    arr[i] = arr[4 - i];
    //    arr[4 - i] = temp;
    //}
    //for (int i = 0; i < 5; i++)
    //{
    //    cout << arr[i] << "\t";
    //}
    //cout << endl;
    system("pause");
    return 0;

}

 

posted @ 2020-02-29 20:25  Jackie_Wang  阅读(484)  评论(0编辑  收藏  举报