绝对值排序

1079: 绝对值排序

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 375  Solved: 193
[Submit][Status][BBS]

Description

将一串数列按其绝对值大小由大到小排列,给出的测试数据不会出现绝对值相同的数

Input

有多组输入,每组首先输入n,代表需要排序的整数个数,n为0时结束

Output

输出排序后的数列,每组结果站一行,整数之间以一个空格分隔,每组数据最后没有空格

Sample Input

7
5 9 0 8 -11 2 -3
5
-4 5 6 9 2
0

Sample Output

-11 9 8 5 -3 2 0
9 6 5 -4 2

HINT 

Source

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
using namespace std;
int cmp(int a,int b)
{
    return abs(a)>abs(b);
}
int main()
{
    int n,a[100],k[100],i;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n,cmp);
        int m=unique(a,a+n)-a;
        for(i=0; i<m-1; i++)
        {
                printf("%d ",a[i]);
        }
        printf("%d
",a[i]);

    }
    return 0;
}
原文地址:https://www.cnblogs.com/tianmin123/p/4685209.html