Blue Bridge Cup training camp array sorting simple questions

Title: an array of N number, Ai denotes an i-th digit. Now type N, l1, r1, l2, r2 represents this number of bits r1 l1 to the ascending order, then the first bits r2 to l2 in descending order. Finally, the output of the array.

#include <stdio.h>

int main()
{
    int n;
    int l1,r1,l2,r2;
    scanf("%d%d%d%d%d",&n,&l1,&r1,&l2,&r2);
    int a[n];
    for(int i=0;i<n;i++)scanf("%d",&a[i]);

    for(int i=l1-1;i<r1-1;i++)   //首次冒泡排序:从小到大
    {
        for(int j=l1-1;j<r1-i+l1-2;j++)
        {
            int temp;
            if(a[j]>a[j+1]){temp=a[j+1];
            a[j+1]=a[j];
            a[j]=temp;}
        }
    }

     for(int i=l2-1;i<r2-1;i++)   //第二次冒泡排序:从大到小
    {
        for(int j=l2-1;j<r2-i+l2-2;j++)
        {
            int temp;
            if(a[j]<a[j+1]){temp=a[j+1];
            a[j+1]=a[j];
            a[j]=temp;}
        }
    }

    printf("%d",a[0]);
    for(int i=1;i<n;i++)
    {
        printf(" %d",a[i]);
    }

    return 0;
}

Ideas: very simple, as the annotation.

Process Reflection:

Reading problems are not serious (cause the first error)

Bubble sort unskilled! ! ! (For the first time or made a logical error) to deepen the learning again to sort an array of ways to see next blog.

Published 10 original articles · won praise 0 · Views 108

Guess you like

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