ORIGIN

HDU-2020 Absolute Sort

ACM 1 mins263 words

Absolute Sort

Input n (n <= 100) integers, and output them after sorting according to the absolute value. The title guarantees that for each test case, the absolute values of all numbers are not equal.

Input

There are multiple groups of input data. Each group occupies one line. The first number of each line is n, followed by n integers. N = 0 means the end of the input data. No processing is performed.

Output

For each test instance, the sorted results are output, separated by a space between the two numbers. One line per test instance.

Sample Input

1
2
3
3 3 -4 2
4 0 1 2 -3
0

Sample Output

1
2
-4 3 2
-3 2 1 0

Analysis

Turn then into positive numbers and sort. And for each unit in array, if it’s negative originally, turn it back to negative.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<bits/stdc++.h>

using namespace std;

map<int,int> a;
int arr[1000];
int main() {
int n;
while(cin >> n, n){
for(int i = 0; i < n; i ++) {
cin >> arr[i];
if(arr[i] >= 0) a[abs(arr[i])] = 0;
else a[abs(arr[i])] = 1;
arr[i] = abs(arr[i]);
}
sort(arr, arr + n, greater<int>());
for(int i = 0; i < n; i ++) {
if(a[arr[i]]) cout << '-' << arr[i];
else cout << arr[i];
if(i != n - 1) cout << " ";
else cout << endl;
}
}
return 0;
}
TOP
COMMENT
  • ABOUT
  • |
o_oyao
  The Jigsaw puzzle is incomplete with even one missing piece. And I want to be the last piece to make the puzzle complete.
Like my post?
Default QR Code
made with ❤️ by o_oyao
©o_oyao 2019-2024

|