-
section5.8알고리즘 2025. 8. 16. 14:10
메디컬 병원 응급실에는 의사가 한 명 밖에 없습니다. 응급실은 환자가 도착한 순서대로 진료를 합니다. 하지만 위험도가 높은 환자는 빨리 응급조치를 의사가 해야 합니다. 이런 문제를 보완하기 위해 응급실은 다음과 같은 방법으로 환자의 진료순서를 정합니다. • 환자가 접수한 순서대로의 목록에서 제일 앞에 있는 환자목록을 꺼냅니다. • 나머지 대기 목록에서 꺼낸 환자 보다 위험도가 높은 환자가 존재하면 대기목록 제일 뒤로 다시 넣습니다. 그렇지 않으면 진료를 받습니다. 즉 대기목록에 자기 보다 위험도가 높은 환자가 없을 때 자신이 진료를 받는 구조입니다. 현재 N명의 환자가 대기목록에 있습니다. N명의 대기목록 순서의 환자 위험도가 주어지면, 대기목록상의 M번째 환자는 몇 번째로 진료를 받는지 출력하는 프로그램을 작성하세요. 대기목록상의 M번째는 대기목록의 제일 처음 환자를 0번째로 간주하여 표현한 것입니다. 입력설명 첫 줄에 자연수 N(5<=N<=100)과 M(0<=M<N) 주어집니다. 두 번째 줄에 접수한 순서대로 환자의 위험도(50<=위험도<=100)가 주어집니다. 위험도는 값이 높을 수록 더 위험하다는 뜻입니다. 같은 값의 위험도가 존재할 수 있습니다. 출력설명 M번째 환자의 몇 번째로 진료받는지 출력하세요. 입력예제 1 5 2 60 50 70 80 90 출력예제 1 3 입력예제 2 6 3 70 60 90 60 60 60 출력예제 2 4ANSWER
public class Question8 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println(solution(n, m, arr)); } public static int solution(int n, int m, int[] arr) { int target = 0; Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < n; i++) { queue.offer(arr[i]); if (i == m) { target = arr[i]; } } int cnt = 0; for (int x : queue) { if (target < x) { cnt ++; } } for (int i = 0; i < m; i++) { if (queue.poll() == target) { cnt ++; } } // 이중 for문 돌면서 값비교해서 순서 내거나 // 하나씩 target이랑 비교하면서 순서 낼 수 있는데 // 문제는 중복된 위험도가 있을 때 어떻게 처리할지? // 같은 숫자인데 구분할 수 가 없음 ... > 규칙이 있는건가,,,? return ++cnt; } }SOLUTION
public class Answer8 { public static void main(String[] args) { Answer8 T = new Answer8(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println(T.solution(n, m, arr)); } public int solution(int n, int m, int[] arr) { int answer = 0; Queue<Person> Q = new LinkedList<>(); for (int i = 0; i < n; i++) { Q.offer(new Person(i, arr[i])); } while (!Q.isEmpty()) { Person tmp = Q.poll(); for (Person x : Q) { if (x.priority > tmp.priority) { Q.offer(tmp); tmp = null; break; } } if (tmp != null) { answer ++; if (tmp.id == m) { return answer; } } } return answer; } } class Person { int id; int priority; public Person(int id, int priority) { this.id = id; this.priority = priority; } }- 객체 생성 대신 map을 넣어서 풀려고 하니까 값 가져오는 게 너무 어려웠다
cf. 그냥 정리하는 것
public class Tmp { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println(solution(n, m, arr)); } public static int solution(int n, int m, int[] arr) { int target = 0; return target; } }public class Tmp { public static void main(String[] args) { Tmp T = new Tmp(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } System.out.println(T.solution(n, m, arr)); } public int solution(int n, int m, int[] arr) { int target = 0; return target; } }- 문제를 풀다 보면 이렇게 두 가지로 기본 틀을 구성할 수 있다
- 전자는 메서드를 static 사용해서 바로 호출하는 것 - static 메서드
- 후자는 클래스 객체를 생성해서 메서드를 호출하는 것 - 인스턴스 메서드
1. static 메서드
- static은 클래스 레벨, 즉, 객체를 생성하지 않고도 클래스명.메서드명()으로 바로 호출
- main 같은 진입점 메서드도 무조건 static
2. 인스턴스 메서드
- 인스턴스 메서드는 반드시 객체를 생성해야 호출
- 객체의 상태를 다룰 수 있기 때문에 객체가 존재해야 그 안에 데이터를 읽고 쓰기 가능
728x90'알고리즘' 카테고리의 다른 글
section6.1 (0) 2025.08.20 리트코드 - 784, 102, 3619 (2) 2025.08.17 section5.7 (0) 2025.08.15 section5.6 (1) 2025.08.15 section5.5 (1) 2025.08.15