BOOK STORE 문제 문의입니다.

  • gnoowik
    gnoowik

    BOOKSTORE

    1. 책은 모두 사야 하기 때문에 각 서점별로 total price 를 구한다.
    2. 모든 적립금에서 가장 적은 적립금을 뺀다. (마지막으로 산 책에 대한 적립금은 사용하지 못하므로, 적립금이 가장 적은 책을 마지막에 산다.)
    3. total price 에서 2번에서 계산된 적립금을 뺀다.

    이런 생각으로 문제를 풀었는데 잘못된 생각인가요?
    예제에 대해서는 정상작동하는데 오답이 뜨네요. 무엇이 문제일까요..
    아래는 소스 코드입니다.

    import java.util.Scanner;
    
    class Book {
        public int price;
        public int point;
    
        public Book(int price, int point) {
            this.price = price;
            this.point = point;
        }
    }
    
    public class Main {
    
        public static void main(String[] args) {
    
            Scanner scanner = new Scanner(System.in);
            int cases = scanner.nextInt(); 
    
            while( cases-- > 0 ) {
                // Do Something
                int N = scanner.nextInt();  // The number of books
                int M = scanner.nextInt();  // The number of stores
    
                Book books[][] = new Book[N][M];
                for(int i=0; i<N; i++) {
                    for(int j=0; j<M; j++) {
                        int price = scanner.nextInt();
                        int point = scanner.nextInt();
                        books[i][j] = new Book(price, point);
                    }
                }
    
    
                int min = Integer.MAX_VALUE;
                for(int i=0; i<M; i++) {
                    int total_price = 0;
                    int total_point = 0;
                    int min_point = Integer.MAX_VALUE;
    
                    for(int j=0; j<N; j++) {
                        total_price += books[j][i].price;
                        total_point += books[j][i].point;
    
                        if(min_point > books[j][i].point) min_point = books[j][i].point; 
                    }
    
                    total_point -= min_point;
    
                    if(min > (total_price - total_point) ) min = (total_price - total_point);
                }
    
                System.out.println(min);
            }
    
        }
    
    }
    

    8년 전
3개의 댓글이 있습니다.
  • JongMan
    JongMan


    쌓인 적립금을 항상 다 쓸 수 있는 것은 아닙니다.


    8년 전 link
  • gnoowik
    gnoowik

    2번에 누락된 내용이 있어 수정했습니다.
    모든 적립금에서 가장 뺀다 -> 모든 적립금에서 가장 적은 적립금을 뺀다.

    다시 답변 부탁드릴게요!


    8년 전 link
  • JongMan
    JongMan

    네. 이 경우에도 위 답변은 성립합니다.

    다음과 같은 책 세 권을 고려해 보세요.

    (가격=10000, 적립금=9900)
    (가격=100, 적립금=10)
    (가격=100, 적립금=10)


    8년 전 link
  • 정회원 권한이 있어야 커멘트를 다실 수 있습니다. 정회원이 되시려면 온라인 저지에서 5문제 이상을 푸시고, 가입 후 7일 이상이 지나셔야 합니다. 현재 문제를 푸셨습니다.