-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java_BigDecimal.java
70 lines (61 loc) · 1.85 KB
/
Java_BigDecimal.java
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
66
67
68
69
70
/*Java's BigDecimal class can handle arbitrary-precision signed decimal numbers. Let's test your knowledge of them!
Given an array, , of real 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 is printed as , and is printed as . If two numbers represent numerically equivalent values (e.g., ), then they must be listed in the same order as they were received as input).
Complete the code in the unlocked section of the editor below. You must rearrange array 's elements according to the instructions above.
Input Format
The first line consists of a single integer, , denoting the number of integer strings.
Each line of the subsequent lines contains a real number denoting the value of .
Output Format
Locked stub code in the editor will print the contents of array to stdout. You are only responsible for reordering the array's elements.
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*/
//SOLUTION//
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];
for(int i=0;i<n;i++){
s[i]=sc.next();
}
sc.close();
Arrays.sort(
s,
0,
n,
Collections.reverseOrder(
new Comparator<String>() {
public int compare(String s1, String s2) {
BigDecimal b1 = new BigDecimal(s1);
BigDecimal b2 = new BigDecimal(s2);
return b1.compareTo(b2);
}
}
)
);
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
}