Algorithm/COS PRO 1급 기출문제

[COS PRO 1급 기출문제 - Java] 2-1 도서 대여점 운영

goakgoak 2020. 12. 21. 15:00

edu.goorm.io/learn/lecture/17301/cos-pro-1%EA%B8%89-%EA%B8%B0%EC%B6%9C%EB%AC%B8%EC%A0%9C-java/lesson/839405/2%EC%B0%A8-%EB%AC%B8%EC%A0%9C1-%EB%8F%84%EC%84%9C-%EB%8C%80%EC%97%AC%EC%A0%90-%EC%9A%B4%EC%98%81-java

 

goorm

구름은 클라우드 기술을 이용하여 누구나 코딩을 배우고, 실력을 평가하고, 소프트웨어를 개발할 수 있는 클라우드 소프트웨어 생태계입니다.

www.goorm.io

 

문제 유형

빈칸 채우기

 

문제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//Book 인터페이스와 ComicBook, Novel 클래스는 Inner Class로 작성되어있습니다. 아래 코드를 잘 읽고 빈칸을 채워주세요.
class Main {
    interface Book{
        public int getRentalPrice(int day);
    }
    
    class Comic ________ {
        public int getRentalPrice(int day){
            int cost = 500;
            day -= 2;
            if(day > 0)
                cost += 200;
            return cost;
        }
    }
    
    class Novel ________ {
        public int getRentalPrice(int day){
            int cost = 1000;
            day -= 3;
            if(day > 0)
                cost += 300;
            return cost;
        }
    }
 
    public int solution(String[] bookTypes, int day) {
        Book[] books = new Book[50];
        int length = bookTypes.length;
        for(int i = 0; i < length++i){
            if(bookTypes[i].equals("comic"))
                books[i] = new ComicBook();
            else if(bookTypes[i].equals("novel"))
                books[i] = new Novel();   
        }
        int totalPrice = 0;
        for(int i = 0; i < length++i)
            totalPrice += books[i].getRentalPrice(day);
        return totalPrice;
    }
 
    // main method {...}
}
 
 
cs

 

풀이

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//Book 인터페이스와 ComicBook, Novel 클래스는 Inner Class로 작성되어있습니다. 아래 코드를 잘 읽고 빈칸을 채워주세요.
class Main {
    interface Book{
        public int getRentalPrice(int day);
    }
    
    class ComicBook implements Book {
        public int getRentalPrice(int day){
            int cost = 500;
            day -= 2;
            if(day > 0)
                cost += (day * 200);
            return cost;
        }
    }
    
    class Novel implements Book {
        public int getRentalPrice(int day){
            int cost = 1000;
            day -= 3;
            if(day > 0)
                cost += (day * 300);
            return cost;
        }
    }
 
    public int solution(String[] bookTypes, int day) {
        Book[] books = new Book[50];
        int length = bookTypes.length;
        for(int i = 0; i < length++i){
            if(bookTypes[i].equals("comic"))
                books[i] = new ComicBook();
            else if(bookTypes[i].equals("novel"))
                books[i] = new Novel();   
        }
        int totalPrice = 0;
        for(int i = 0; i < length++i)
            totalPrice += books[i].getRentalPrice(day);
        return totalPrice;
    }
 
  // 아래는 테스트케이스 출력을 해보기 위한 main 메소드입니다.
    public static void main(String[] args) {
        Main sol = new Main();
        String[] bookTypes = {"comic""comic""novel"};
        int day = 4;
        int ret = sol.solution(bookTypes, day);
 
        // [실행] 버튼을 누르면 출력 값을 볼 수 있습니다.
        System.out.println("solution 메소드의 반환 값은 " + ret + " 입니다.");
    }
}
 
 
cs