https://www.acmicpc.net/problem/10814
// ArrayList에다 차례대로 넣으면 먼저 온 순서대로는 맞춰지고 나이에 따라 정렬해주면 되겠다!
// 프로덕션코드
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Yun_10814 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberOfMember = scanner.nextInt();
scanner.nextLine();
Member[] members = new Member[numberOfMember];
for (int i = 0; i < numberOfMember; i++) {
String[] input = scanner.nextLine().split(" ");
members[i] = new Member(input[0], input[1]);
}
Arrays.sort(members, new Comparator<Member>() {
@Override
public int compare(Member o1, Member o2) {
if (o1.age != o2.age) {
return o1.age - o2.age;
}
return -1;
}
});
for (int i = 0; i < numberOfMember; i++) {
System.out.println(members[i].age + " " + members[i].name);
}
}
}
class Member {
int age;
String name;
Member(int age, String name) {
this(Integer.toString(age), name);
}
Member(String age, String name) {
this.age = Integer.parseInt(age);
this.name = name;
}
}
// 실패? Arrays.sort하면 먼저 온 순서까지 뒤틀려 버리네? 메소드 자세히 살펴보자!
......? LegacyMergeSort...? TimSort? 이해하기에는 내공이 딸린다... 일단 순서가 유지되지 않는다는 것만 알아두자..
// 그럼 sort 메소드 안에 compare 메소드 구현할때 두 조건을 동시에 넣어야한다는 뜻인데...
// 처음 멤버 객체 만들어줄때 순서도 같이 넣어주자! 그리고 나이같을 경우에는 순서 고려!
//프로덕션코드
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Yun_10814 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberOfMember = scanner.nextInt();
scanner.nextLine();
Member[] members = new Member[numberOfMember];
for (int i = 0; i < numberOfMember; i++) {
String[] input = scanner.nextLine().split(" ");
members[i] = new Member(input[0], input[1], i);
}
Arrays.sort(members, new Comparator<Member>() {
@Override
public int compare(Member o1, Member o2) {
if (o1.age != o2.age) {
return o1.age - o2.age;
}
return o1.order - o2.order;
}
});
for (int i = 0; i < numberOfMember; i++) {
System.out.println(members[i].age + " " + members[i].name);
}
}
}
class Member {
int age;
String name;
int order;
Member(int age, String name, int order) {
this(Integer.toString(age), name, order);
}
Member(String age, String name, int order) {
this.age = Integer.parseInt(age);
this.name = name;
this.order = order;
}
}
// 성공! 리팩토링하자
// 프로덕션코드
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Yun_10814 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numberOfMember = scanner.nextInt();
scanner.nextLine();
Member[] members = new Member[numberOfMember];
setmembers(scanner, numberOfMember, members);
sorting(members);
print(numberOfMember, members);
}
private static void print(int numberOfMember, Member[] members) {
for (int i = 0; i < numberOfMember; i++) {
System.out.println(members[i].age + " " + members[i].name);
}
}
private static void sorting(Member[] members) {
Arrays.sort(members, new Comparator<Member>() {
@Override
public int compare(Member o1, Member o2) {
if (o1.age != o2.age) {
return o1.age - o2.age;
}
return o1.order - o2.order;
}
});
}
private static void setmembers(Scanner scanner, int numberOfMember, Member[] members) {
for (int i = 0; i < numberOfMember; i++) {
String[] input = scanner.nextLine().split(" ");
members[i] = new Member(input[0], input[1], i);
}
}
}
class Member {
int age;
String name;
int order;
Member(int age, String name, int order) {
this(Integer.toString(age), name, order);
}
Member(String age, String name, int order) {
this.age = Integer.parseInt(age);
this.name = name;
this.order = order;
}
}
// 성공!
'코딩테스트연습 > 백준' 카테고리의 다른 글
[백준 - JAVA] 2775번 부녀회장이 될테야 (0) | 2021.12.18 |
---|---|
[백준 - JAVA] 2292번 벌집 (0) | 2021.12.18 |
[백준 - JAVA] 1181번 단어 정렬 (0) | 2021.12.15 |
[백준 - JAVA] 11650번 좌표 정렬하기 (0) | 2021.12.15 |
[백준 - JAVA] 10989 수 정렬하기 3 (0) | 2021.12.14 |