[BOJ 1106] 호텔 JAVA

popolarburr·2023년 4월 20일
0
post-thumbnail

- 문제



- 풀이 1(실패)


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

public class Dp1106 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int min_gain = Integer.parseInt(st.nextToken());
        int city = Integer.parseInt(st.nextToken());
        int[] arr = new int[min_gain + 1];

        for (int i = 0; i < city; i++) {
            st = new StringTokenizer(br.readLine());
            int cost = Integer.parseInt(st.nextToken());
            int people = Integer.parseInt(st.nextToken());

            int idx = 1;
            for (int j = 1; j <= min_gain; j++) {
                if (j % cost == 0 && arr[j] == 0) {
                    arr[j] = people * idx++;
                } else {
                    if (arr[j] == 0) { //
                        arr[j] = (j / cost) * people;
                    } else { // 이미 다른사람이 있는 상태일 때
                        arr[j] += (j / cost) * people;
                    }
                }
            } // end of small F-loop
        } // end of big F-loop
        for (int j = 1; j <= min_gain; j++) {
            System.out.println(j + " " + arr[j]);
        }

    }
}

- 정리1 : 실패사유

DP를 연습하고자 시작했던 문제이다. 그러나, 처음엔 감이 안와서 모든 결과값을 만들어놓고 구하고자 하는 답을 찾아서 제출하는 방식으로 하려했다. 근데 이는 당연하게도 DP와는 전혀 맞지않는 풀이이고, 풀다가 이러게 푸는 것이 아니라고 느껴 다시 풀기로 마음 먹었다. DP의 기초부터 차근차근 다시 해보기위해!!


풀이 2 - (진행중)

profile
차곡차곡

0개의 댓글