1. 程式人生 > >數組的基本操作

數組的基本操作

close sed 查找 end pre for display lose style

  1. 查找一個數是否再數組裏 技術分享
    int x;
    cin>>x;
    int a[110]
    for(int i=0;i<n;i++)
        if(a[i]==x)cout<<i<<endl;
    View Code

  2. 刪除某個元素 技術分享
    int k;
    cin>>k;
    int a[110];
    for(int i=k;i<n;i++)
        a[i]=a[i+1];
    n--;
    View Code

  3. 插入某個元素 技術分享
    for(int i=n;i>=k;i--)
        a[i+1]=a[i];
    a[k]=x;
    n++;
    View Code

  4. 數組的平移

    技術分享
    int temp=a[1];
    for(int i=1;i<n;i++)
        a[i]=a[i+1];
    a[n]=temp;
    View Code

數組的基本操作