2018-计算机系机试(第二批)-E-绝对值排序

单点时限: 2.0 sec

内存限制: 256 MB

输入 n 个整数,按照绝对值从大到小排序。绝对值相等的整数按照整数值从小到大排序。
例如:
3 个整数 -22-6 的排序结果为 -6, -2, 2

输入格式

第一个数是 n (2n20 ),后面是 n 个整数(值范围为109109 )。n+1 个整数之间都有一个空格。

输出格式

按排序后的顺序输出这些数。相邻两个数之间用逗号分隔。

样例

Input
3 -2 2 -6
Output
-6,-2,2
Input
4 1 2 21 11
Output
21,11,2,1
Input
4 -1 5 10 10
Output
10,10,5,-1
 1 #include<stdio.h>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<cmath>
 5 #include<algorithm>
 6 using namespace std;
 7 bool cmp(int a,int b)
 8 {
 9     if(abs(a)!=abs(b))
10         return abs(a)>abs(b);
11     else 
12         return a<b;
13 }
14 
15 int main()
16 {
17     int n;
18     int a[25];
19     scanf("%d",&n);
20     for(int i=0;i<n;i++)
21         scanf("%d",&a[i]);
22     sort(a,a+n,cmp);
23     for(int i=0;i<n-1;i++)
24     {
25         printf("%d,",a[i]);
26     }
27     printf("%d",a[n-1]);
28     return 0;
29     
30 }
原文地址:https://www.cnblogs.com/Annetree/p/10563375.html