스터디사이트 : https://www.hackerrank.com



JAVA Basic : 중첩 로직



중첩 로직을 이용하여 도서대여&반납 일자에 따른 연체료 계산에 대하여 코딩하라.


※ 조건

1. 만약 책이 제때 반납되거나 반납기한 이전에 반납이 된다면, 연체료(fine)는 0원


2. 만약 책 실제 반납날짜가 예상 반납기한 을 지나 반납되었으나 반납기한과 같은 년도이라면, 

(ex. 반납기한 : 2016/10/02, 실제반납날짜 : 2016/10/11)

 연체료(fine) = 15 (원) x {연체날짜} 

3. 만약 책 실제 반납날짜가 예상 반납기한 을 지나 반납되었으나 같은 년도에 반납이 되었다면, 

(ex. 반납기한 : 2016/10/02, 실제반납날짜 : 2016/11/11)

 연체료(fine) = 500 (원) x {연체월수} 

4. 만약 책 실제 반납날짜가 예상 반납기한 을 지나 반납되었다면, 

 연체료(fine) = 10,000 (원) 

5. 변수 범위

년 : 1 ~ 3000

월 : 1 ~ 12

일 : 1 ~ 31


입력

9 6 2015        ← 실제 반납날짜

6 6 2015        ← 예상 반납날짜

출력

45


계산법

예상날짜 반납날짜의 수 비교는,

년 , 월 , 일의 조건에 따라 다르게 비교한다.

위의 입력의 예를 들어 비교하면,

위의 조건 2번에 해당하며,

2-1. 실제 년도 == 예상 년도

2-2. 실제 월 == 예상 월

2-3. 실제 일 > 예상 일 (2-1번과 2-2번 조건이 성립해야 함)


계산  15 x { (실제 일) - (예상 일) }

= 15 x (9 - 6)

= 45



디버깅 1

조건을 여러 개 주려면,

범위를 큰 것 부터 좁은 순서대로 순차적으로 내려가야 한다.

그래서 가장 범위가 넓은 년도 비교부터 시작해서 (조건4),

년도 & 월 (조건3) -> 년도 & 월 & 일 (조건2) 순서대로 조건을 비교 해 보었다.

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
public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 실제 반납날짜
        int a_day = scan.nextInt();
        int a_month = scan.nextInt();
        int a_year = scan.nextInt();
        
        // 예상 반납날짜
        int e_day = scan.nextInt();
        int e_month = scan.nextInt();
        int e_year = scan.nextInt();
        
        // 연체료
        int fine=0;
        if(a_year != e_year){   // 조건 4 : 반납기한 년을 지나 반납됨
            System.out.println("조건4");
            fine=10000;
        }else if(a_month > e_month){    // 조건 3 : (년은 같고,)반납기한 월을 지나 반납됨
            System.out.println("조건3");
            
            fine=500 * (a_month - e_month);
        }else if(a_day > e_day){     // 조건 2 : (년과 월이 같고,)반납기한 일을 지나 반납됨
            System.out.println("조건2");
            fine=15 * (a_day - e_day);
        }
        
        System.out.println(fine);                
    }
 
cs

결과 : 

조건2

45

▷ Feedback :

결과 돌려보다가 에러나는 케이스 몇 개가 있다.

입력1

Test Case #3

  31 12 2009

  1 1 2010

출력1

  0

입력2

Test Case #7

  23 12 1234

  19 9 2468

출력2

  0

입력3

Test Case #5

  1 1 1

  8 8 8

출력3

  0






에러 케이스 모두 다

입력된 실제반납날짜가 예상반납날짜보다 이전에 반납된 것들이다..

조건 1에 대한 if식을 안 넣어놨네?

내가 블로그질하면서 연체료 푸른색 음영을 안줬다고 내가 쓴 글을 내가 무시했다..ㅋ




디버깅 2

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
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 실제 반납날짜
        int a_day = scan.nextInt();
        int a_month = scan.nextInt();
        int a_year = scan.nextInt();
        
        // 예상 반납날짜
        int e_day = scan.nextInt();
        int e_month = scan.nextInt();
        int e_year = scan.nextInt();
        
        // 연체료
        int fine=0;
        if(a_year != e_year){   // 조건 4 : 반납기한 년을 지나 반납됨
            //System.out.println("조건4");
            fine=10000;
        }else if(a_month > e_month){    // 조건 3 : (년은 같고,)반납기한 월을 지나 반납됨
            //System.out.println("조건3");
            fine=500 * (a_month - e_month);
        }else if(a_day > e_day){     // 조건 2 : (년과 월이 같고,)반납기한 일을 지나 반납됨
            //System.out.println("조건2");
            fine=15 * (a_day - e_day);
        }else{ // 조건 1 : (년과 월이 같고,)반납기한 일을 지나 반납됨
            //System.out.println("조건1");
            fine=0;
        }        
        System.out.println(fine);                
    }
cs

입력 : 

31 12 2009

1 1 2010


결과 : 

10000  <<-- 틀렸다 X

▷ Feedback :

위의 결과가 왜 틀렸냐 하면..

입력된 실제반납날짜가 예상반납날짜보다 이전에 반납된 것이면 0원이여야 하는데 연체료가 발생했기 때문이다.


이 이유는 조건 4에 대한 if문이 잘못됐기 때문이다..

단순히 a_year와 e_year만 다르면 조건 4가 걸리는건데 저러면 안된다는걸 알았네?

a_year < e_year 이면 실제반납년도가 예상반납년도보다 작으니까 일찍 반납했다는 거를 알 수 있지..

머리 굴리기 힘들다.


디버깅 3

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
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 실제 반납날짜
        int a_day = scan.nextInt();
        int a_month = scan.nextInt();
        int a_year = scan.nextInt();
        
        // 예상 반납날짜
        int e_day = scan.nextInt();
        int e_month = scan.nextInt();
        int e_year = scan.nextInt();
        
        // 연체료
        int fine=0;
        if(a_year > e_year){   // 조건 4 : 반납기한 년을 지나 반납됨
            System.out.println("조건4");
            fine=10000;
        }else if(a_month > e_month){    // 조건 3 : (년은 같고,)반납기한 월을 지나 반납됨
            System.out.println("조건3");
            fine=500 * (a_month - e_month);
        }else if(a_day > e_day){     // 조건 2 : (년과 월이 같고,)반납기한 일을 지나 반납됨
            System.out.println("조건2");
            fine=15 * (a_day - e_day);
        }else{
            System.out.println("조건1");
            fine=0;
        }        
        System.out.println(fine);                
    }
cs

입력 : 

31 12 2009

1 1 2010


결과 : 

조건3

5500

▷ Feedback :

하아 죄송합니다

취준의 여파입니다

머리 못굴려서 죄송합니다 ^^;


애초에 그냥 반납을 제때 했냐 안했냐 물어봐서 

Y : 그래 너 0원

N : 너 얼마나 연체했나 보자 너 연체료 내야돼


...


결과

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
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        // 실제 반납날짜
        int a_day = scan.nextInt();
        int a_month = scan.nextInt();
        int a_year = scan.nextInt();
        
        // 예상 반납날짜
        int e_day = scan.nextInt();
        int e_month = scan.nextInt();
        int e_year = scan.nextInt();
        
        // 연체료
        int fine=0;
        if(a_year < e_year){
            //System.out.println("조건1");
            fine=0;
        }else{
            if(a_year > e_year){   // 조건 4 : 반납기한 년을 지나 반납됨
                //System.out.println("조건4");
                fine=10000;
            }else if(a_month > e_month){    // 조건 3 : (년은 같고,)반납기한 월을 지나 반납됨
                //System.out.println("조건3");
                fine=500 * (a_month - e_month);
            }else if(a_day > e_day){     // 조건 2 : (년과 월이 같고,)반납기한 일을 지나 반납됨
                //System.out.println("조건2");
                fine=15 * (a_day - e_day);
            }
        }              
        System.out.println(fine);                
    }
cs

입력 : 

31 12 2009

1 1 2010


결과 : 

0


+ Recent posts