반응형
1431번: 시리얼 번호
첫째 줄에 기타의 개수 N이 주어진다. N은 1,000보다 작거나 같다. 둘째 줄부터 N개의 줄에 시리얼 번호가 하나씩 주어진다. 시리얼 번호의 길이는 최대 50이고, 알파벳 대문자 또는 숫자로만 이루
www.acmicpc.net
java8의 스트림을 이용하여 풀었습니다.
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
String[] serial = new String[N];
while (N-->0) serial[N] = br.readLine();
Arrays.stream(serial).sorted(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1.length()==o2.length()){
int fst = Arrays.stream(o1.split(""))
.filter(str->str.matches("^[0-9]+$"))
.mapToInt(Integer::parseInt)
.sum();
int sec = Arrays.stream(o2.split(""))
.filter(str->str.matches("^[0-9]+$"))
.mapToInt(Integer::parseInt)
.sum();
if (fst==sec) return o1.compareTo(o2);
return fst-sec;
}
return o1.length()-o2.length();
}
}).forEach(str->sb.append(str+"\n"));
System.out.print(sb);
}
}
반응형
'Algorithm' 카테고리의 다른 글
백준 10757번 큰 수 A+B [ Java ] (0) | 2021.01.04 |
---|---|
백준 3273번 두 수의 합 [ Java ] (0) | 2021.01.03 |
백준 10825번 국영수 [ Java ] (0) | 2021.01.03 |
백준 10813번 공 바꾸기 [ Java ] (0) | 2021.01.03 |
백준 10819번 차이를 최대로 [ Java ] (0) | 2021.01.03 |