[구현] 3230번 - 금메달, 은메달, 동메달은 누가?

안수진·2023년 10월 17일

Baekjoon

목록 보기
9/55
post-thumbnail

🔗 문제 링크

백준 3230번 - 금은동

📝 풀이

LinkedList의 void add(int index, Object element) 메소드를 활용하면 된다.
지정된 위치(index)에 객체를 저장한다.
자리에 있던 기존의 데이터는 뒤로 밀려나기만 할 뿐 삭제되지 않는다.
ex) add(2, Object) -> 기존, 기존, 추가, 기존, 기존

그리고 등수를 index로 생각하면 정말정말 쉽게 풀리는 문제

정작 나는 선수번호를 index로, index에 해당하는 값을 등수로 생각했고
int형 배열로 접근해서 몇시간 동안 삽질을 했다..🤣

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main{
	
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int N = Integer.parseInt(st.nextToken());
		int M = Integer.parseInt(st.nextToken());
		
		LinkedList<Integer> player = new LinkedList<>();
		for(int i = 0; i < N; i++) {
			int score = Integer.parseInt(br.readLine()) - 1; // 등수 = 인덱스
			player.add(score, i);
		}
		
		LinkedList<Integer> finalPlayer = new LinkedList<>();
		for(int j = M; j > 0; j--) {
			int num = player.get(j-1);
			int score = Integer.parseInt(br.readLine()) - 1;
			finalPlayer.add(score, num);
		}
		
		for(int i = 0; i < 3; i++) {
			System.out.println(finalPlayer.get(i)+1);
		}
		
	}
}
profile
항상 궁금해하기

0개의 댓글