Java BigDecimal
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 Style
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 .
Constraints
1<= n <=200
Each si has at most 300 digits.
Output Style
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
Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.
Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.
HAPPY CODING 😁
( Java )
import java.math.BigDecimal;
import java.util.*;
class Solution{
public static void main(String []args){
//Input
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();
for(int i=0;i<n;i++)
{
//inserting string values to bigdecimal
BigDecimal First=new BigDecimal(s[i]);
int index=i;
for(int j=i+1;j<n;j++)
{
//second BigDecimal to compare the first Bigdecimal
BigDecimal Second=new BigDecimal(s[j]);
//comparing if First element is greater that second element
//if the First element is greater than Second element than compareTo() returns 1
if(Second.compareTo(First)==1){
First=Second;
index=j;
}
}
//temporary variable to store s[i] value
String temp=s[i];
s[i]=s[index];
s[index]=temp;
}
//Output
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
}