Sorting the absolute value of the electric Hang hdu2020 (C language bubbling method)

Sort absolute value

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 131989 Accepted Submission(s): 62099

Problem Description
input n (n <= 100) integers, in accordance with the absolute value of the output descending order.To ensure that each test subject for example, the absolute value of all not equal

Input
input multiple sets of data, each representing one line, the first number of each line is n, then n is an integer, n = 0 indicates the end of input data, without processing.

Output
For each test case, the output of sorting results,Separated by a space between the two numbers. It contains one row for each test case

Sample Input
3 3 -4 2
4 0 1 2 -3
0

Sample Output
-4 3 2
-3 2 1 0

# include<stdio.h>
# include<math.h>
int main()
{
    int n,a[100],i,j,t;
    while((scanf("%d",&n))!=EOF&&(n>=0&&n<=100))
    {
        if(n==0)
            return 0;
        else
        {
            for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            for(i=0;i<n-1;i++)
                for(j=0;j<n-i-1;j++)
            {
                if(abs(a[j])<abs(a[j+1]))
                {
                   t=a[j+1];
                   a[j+1]=a[j];
                   a[j]=t;
                }
            }
            for(j=0;j<n-1;j++)
                printf("%d ",a[j]);
            printf("%d",a[n-1]);
            printf("\n");

        }

    }
return 0;
}

Published an original article · won praise 0 · Views 16

Guess you like

Origin blog.csdn.net/Beau_li/article/details/104089760