• 顺序表元素位置倒置示例c++实现


    #include <iostream>

    #define MAXSIZE 100

    using namespace std;

    void reverse(int a[],int n)//对数组元素置换的函数,n表示要置换的元素个数

    {

        if(n>MAXSIZE){cout<<"超出数组边界,元素位置置换失败";return;}

        else

        {

            for(int i=0;i<n/2;i++)

            {

                //a[i]a[n-i-1]互换,即头尾两两互换

                int t=a[i];

                a[i]=a[n-i-1];

                a[n-i-1]=t;

            }

        }

    }

    int main()

    {

        int arr[MAXSIZE]={14,15,16,17,18,19,20};

        cout<<"原数组是:";

        for(int i=0;i<7;i++)cout<<arr[i]<<" ";

        reverse(arr,7);

        cout<<" 置换后的数组是:";

        for(int i=0;i<7;i++)cout<<arr[i]<<" ";

        return 0;

    }

    运行结果:

     

  • 相关阅读:
    sshpass做秘钥分发,ansible做自动化运维工具
    Day7 面向对象和类的介绍
    R-aggregate()
    R-seq()
    R-ts()
    R-ets()
    python-无联网情况下安装skt-learn
    python-线性回归预测
    python-matplotlib-ERROR
    python-pyhs2
  • 原文地址:https://www.cnblogs.com/linruier/p/7521130.html
Copyright © 2020-2023  润新知