Blue Bridge Cup exam to check reordering array problem

topic

Defining a number of the input array, a reserved value of the same, and different values ​​sorted by size, the final output.

Example O: 10   

                          20 40 32 67 78 89 56 23 1 5 20

                          9

                          1 5 20 23 32 40 56  67 78 89

#include <stdio.h>

int main()
{
    int num=0;
    scanf("%d",&num);
    int a[num];
    for(int i=0; i<num; i++)
    {
        scanf("%d",&a[i]);
    }                                   //读取数组

    for(int i=0; i<num-1; i++)          //查重操作
    {
        for(int j=i+1; j<num; j++)
        {
            int desk;
            desk=a[i];
            if(desk==a[j])
            {
                a[j]=-1;
            }
        }
    }

    for(int i=0; i<num-1; i++)          //冒泡排序
    {
        for(int j=0; j<num-i-1; j++)
        {
            int c;
            if(a[j]>a[j+1])
            {
                c=a[j+1];
                a[j+1]=a[j];
                a[j]=c;
            }
        }
    }

    int num2=0;                        //计数并且输出
    int flag=0;
    for(int i=0; i<num; i++)
    {
        if(a[i]!=-1)num2++;
    }
    printf("%d\n",num2);
    for(int i=0; i<num; i++)
    {
        if(flag==1)printf(" ");
        if(a[i]!=-1)
        {
            flag=1;
            printf("%d",a[i]);
        }

    }

    return 0;
}

Thinking Description: simpler, as shown in code comments

Process Reflection: 

Ideas have a weight check innovations: the use of alternative -1 redundant duplicate values. (The latter does not affect the size sorting and easy filtering)

Method bubble unskilled Application Error ( determining position values bubbling method must be started from the head end to determine !) (And two cycles)

Finally, there are innovative control the output format (use the FLAG control space problems) later met such an output format "568945231564" can refer to the method or the first output of the idea of ​​space as a blank output can not afford to eat! !

 

 

 

 

Published 10 original articles · won praise 0 · Views 109

Guess you like

Origin blog.csdn.net/weixin_45076393/article/details/104523319