File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .io .BufferedReader ;
2+ import java .io .IOException ;
3+ import java .io .InputStreamReader ;
4+ import java .util .ArrayList ;
5+ import java .util .List ;
6+
7+ public class Main {
8+
9+ public static void main (String [] args ) throws IOException {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
11+ int t = Integer .parseInt (br .readLine ());
12+
13+ List <String > list = new ArrayList <>();
14+
15+ while (t -- > 0 ) {
16+ list .add (br .readLine ());
17+ }
18+
19+ list .sort ((o1 , o2 ) -> {
20+ if (o1 .length () != o2 .length ()) {
21+ return o1 .length () - o2 .length ();
22+ } else {
23+ int a = 0 ;
24+ int b = 0 ;
25+ for (int i = 0 ; i < o1 .length (); i ++) {
26+ int num1 = o1 .charAt (i ) - '0' ;
27+ int num2 = o2 .charAt (i ) - '0' ;
28+
29+ if (num1 > 0 && num1 < 10 ) {
30+ a += num1 ;
31+ }
32+ if (num2 > 0 && num2 < 10 ) {
33+ b += num2 ;
34+ }
35+ }
36+ if (a == b ) {
37+ return o1 .compareToIgnoreCase (o2 );
38+ }
39+ return a - b ;
40+ }
41+ });
42+
43+ list .forEach (System .out ::println );
44+ }
45+
46+ }
You can’t perform that action at this time.
0 commit comments