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 can see a lot of elegant solutions here, but I did it with a custom class
importjava.io.*;importjava.util.*;importjava.math.BigDecimal;publicclassSolution{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerscanner=newScanner(System.in);TreeSet<FakeDecimal>set=newTreeSet<>();intn=Integer.parseInt(scanner.nextLine());for(inti=0;i<n;i++){Stringline=scanner.nextLine();FakeDecimalitem=newFakeDecimal(line,i);set.add(item);}Iterator<FakeDecimal>iterator=set.descendingIterator();while(iterator.hasNext()){System.out.println(iterator.next());}}}classFakeDecimalimplementsComparable<FakeDecimal>{privatefinalBigDecimalnumber;privatefinalStringrepr;privatefinalintorder;publicFakeDecimal(StringnumberString,intorder){this.repr=numberString;this.number=newBigDecimal(numberString);this.order=order;}@OverridepublicStringtoString(){returnrepr;}@OverridepublicintcompareTo(FakeDecimalother){if(this.number.compareTo(other.number)==0){returnInteger.compare(other.order,this.order);}else{returnthis.number.compareTo(other.number);}}@Overridepublicbooleanequals(Objectobj){if(objinstanceofFakeDecimal){FakeDecimalother=(FakeDecimal)obj;returnthis.order==other.order&&this.repr.equals(other.repr);}else{returnfalse;}}@OverridepublicinthashCode(){intresult=17;result=31*result+order;result=31*result+number.hashCode();result=31*result+repr.hashCode();returnresult;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java BigDecimal
You are viewing a single comment's thread. Return to all comments →
I can see a lot of elegant solutions here, but I did it with a custom class