We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I really can't understand why test case 0 is wrong with the code i wrote. It seems that it keeps switching 0.12 with .12 and I don't know why. I would really appreciate if someone could explain it to me.
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=1; i<n; i++){
for(int j=0; j<n; j++){
BigDecimal bd1 = new BigDecimal(s[i]);
BigDecimal bd2= new BigDecimal(s[j]);
int smaller=bd1.compareTo(bd2);
String tempS;
if(smaller==0)
continue;
else if(smaller==1)
{
tempS=s[i];
s[i]=s[j];
s[j]=tempS;
}
}
}
//Output
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
Java BigDecimal
You are viewing a single comment's thread. Return to all comments →
I really can't understand why test case 0 is wrong with the code i wrote. It seems that it keeps switching 0.12 with .12 and I don't know why. I would really appreciate if someone could explain it to me.
import java.math.BigDecimal; import java.util.*; class Solution{
}
Check your output: your result puts .12 before 0.12, but it's wrong since it's required that:
So, the sorting algorithm you used is not stable. I tried to fix your code, using the Bubble Sort: