-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigDecimal_Comparator
More file actions
69 lines (54 loc) · 1.42 KB
/
BigDecimal_Comparator
File metadata and controls
69 lines (54 loc) · 1.42 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Java's BigDecimal class can handle arbitrary-precision signed decimal numbers.
Given an array, s, of n number strings, sort them in descending order — but wait, there's more!
Each number must be printed in the exact same format as it was read from stdin,
meaning that .1 is printed as .1, and 0.1 is printed as 0.1.
If two numbers represent numerically equivalent values (e.g., .1 = 0.1),
then they must be listed in the same order as they were received as input).
Sample Input:
9
-100
50
0
56.6
90
0.12
.12
02.34
000.000
Sample Output:
90
56.6
50
02.34
0.12
.12
0
000.000
-100
*/
import java.math.BigDecimal;
import java.util.*;
class Solution {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] s = new String[n+2]; //2 is added purposely to see if you check errors
for(int i=0;i<n;i++){
s[i] = sc.next();
}
sc.close();
class Checker implements Comparator<String> {
@Override
public int compare(String a, String b) {
if(a==null || b==null) return 0;
return (new BigDecimal(b)).compareTo(new BigDecimal(a));
}
}
Arrays.sort(s, new Checker());
for (int i=0;i<n;i++) // for (String st : s) prints two "null" at the end, so don't use it
{
System.out.println(s[i]);
}
}
}