从数组中插入一个数

#include<stdio.h>

int main()

{

    int a[6]={2,4,6,8,10};

    int i,j,k;

   

    printf ("输入一个数:");

    scanf ("%d",&k);

    printf("原来的数组:\n");

    for(i=0;i<=4;i++)

    {

    printf("%d\t",a[i]);

    }

    printf("\n");

    for(i=0;i<=3;i++)

    {

                if(k>a[i]&&k<a[i+1])

         {

                        for(j=5;j>i+1;j--)

                    {

                                a[j]=a[j-1];

                    }

                    a[i+1]=k;

        }

                else

                {

                if(k<a[0])

                {

                        for(j=5;j>i;j--)

                       {

                            a[j]=a[j-1];

                        }

                        a[0]=k;

                }

                else if(k>a[4])

                a[5]=k;

                    else if(k==a[i])

                    {

                                for(j=5;j>i;j--)

                     {

                                    a[j]=a[j-1];

                        }

                            a[i]=k;

                    }

                }

        }

            printf("现在的数组:\n");

            for(i=0;i<=5;i++)

        {

                    printf("%d\t",a[i]);

        }

        return 0;

}

已知一个由小到大的数组,插入一个数要求按原来规则排序。

qq_四乐_0
浏览 1651回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP