Java’s BigDecimal class can handle arbitrary-precision signed decimal numbers. Let’s test your knowledge of them!
Given an array, s, of n 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 .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).
Complete the code in the unlocked section of the editor below. You must rearrange array s‘s elements according to the instructions above.
Input Format
The first line consists of a single integer, n, denoting the number of integer strings.
Each line i of the n subsequent lines contains a real number denoting the value of s-ith.
Constraints
- Each s-ith has at most 300 digits.(so using BigDecimal)
Output
Reordering of 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.java
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]; //input for(int i=0;i<n;i++){ s[i]=sc.next(); } sc.close(); //sorting Arrays.sort(s,0,n,new Comparator<object>(){ public int compare(Object a1, Object a2) { BigDecimal bigDec1 = new BigDecimal((String)a1); BigDecimal bigDec2 = new BigDecimal((String)a2); return bigDec2.compareTo(bigDec1); } }); //output for(int i=0;i<n;i++){ System.out.println(s[i]); } } }
Thank You.
Arrays.sort(s,0,n,new Comparator()
Thanks For Helping .I will get Back with the corrected Solution.
Cheers, great stuff, I enjoying.
Thank You. If You would like any more ,please suggest the topic.I will definitely try to get on it. Keep Visiting.
Thanks A Lot!! Keep Visiting..
I see something truly special in this web site.
Thanks ..Keep Visiting!!
ost but I was wondering if you could write a litte more on this subject? I’d be very grateful if you could elaborate a little bit further. Kudos!
Thanks I will get back to you with more content. Keep Surfing!!!
Greetings! Very useful advice in this particular article!
It’s the little changes that make the largest changes. Many
thanks for sharing!