728x90
반응형
2024.05.07기준 - 실버4
백준, BEAKJOON, BOJ, JAVA, 자바
풀이
이 문제는 아파트 게임을 이용한 문제입니다.
중요한 점은 참가자의 두 손의 높이 h1, h2가 공백으로 주어지는데,
층 수에 맞춰서 주어지는게 아닌 1이상 10000이하로 주어진다는 점을 생각해야 됩니다.
사람의 번호와 그 사람의 층 수를 기록하기 위해 새로운 class인 Member를 선언했습니다.
선언된 Member로 List를 생성해 입력을 받은 뒤,
List를 Member의 floor의 기준으로 오름차순 정렬을 해주었습니다.
만약, n % (전체 층 수)가 0이면 최대 층이 나오게 아니면 나머지가 나오게 변수를 입력 받았습니다.
그러고 해당 충 수의 Member의 number를 반환하는 코드를 작성했습니다.
코드
import java.io.*;
import java.util.*;
public class Main {
static public class Member {
int number, floor;
public Member(int n, int f) {
number = n;
floor = f;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken()); // 아파트 층 수.
int m = Integer.parseInt(st.nextToken()); // 참가자의 수.
int max = m * 2; // 최대 층 수.
int result = 0;
List<Member> list = new LinkedList<>();
list.add(new Member(0, 0));
for (int i = 1; i <= m; i++) {
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int l = Integer.parseInt(st.nextToken());
list.add(new Member(i, r));
list.add(new Member(i, l));
}
// 층 수에 따라 정렬.
Collections.sort(list, (o1, o2) -> o1.floor - o2.floor);
// 나머지가 0이면 max값을 아니면 나머지 값을 입력.
int floor = n % max == 0 ? max : n % max;
// 해당 층 수의 number 입력.
result = list.get(floor).number;
bw.write(Integer.toString(result));
bw.flush();
bw.close();
br.close();
}
}
728x90
반응형
'코딩테스트 일기 (BAEKJOON)' 카테고리의 다른 글
BAEKJOON / 백준 - JAVA 9506번 약수들의 합 (0) | 2024.05.08 |
---|---|
BAEKJOON / 백준 - JAVA 31796번 한빛미디어 (Easy) (0) | 2024.05.07 |
BAEKJOON / 백준 - JAVA 15917번 노솔 방지문제야!! (0) | 2024.05.07 |
BAEKJOON / 백준 - JAVA 31780번 불사조 (0) | 2024.05.02 |
BAEKJOON / 백준 - JAVA 2847번 게임을 만든 동준이 (2) | 2024.05.01 |