알고리즘 풀이/SWEA

[SWEA][JAVA] 2056. 연월일 달력

배게 2021. 6. 6. 02:53
728x90

Scanner랑 Buffered 차이 까먹음

 

1. String으로 받은 입력값이 8자리인가

2. month부분의 값이 1~12에 속해 있는가

3. days부분이 해당 케이스의 month의 범위 내에 존재하는 days인가

 

조건이 만족하지 않을 경우 -1로 예외처리하고

만족할 경우 parseInt할 때 사라진 0값 때문에 숫자를 그대로 출력하면 안되고

다시 substring으로 year,month,days 문자 따줌

 

 

 

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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Solution {
    private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
    private static int[] daysInMonth = {31,28,31,30,31,30,31,31,30,31,30,31}; 
    
    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(br.readLine());
        
        
        for(int i=0; i<T; i++) {
            String input = br.readLine();
            if(input.length()!=8) {
                bw.write("#"+(i+1)+" -1\n");
                continue;
            }
            
            int year = Integer.parseInt(input.substring(0,4));
            int month = Integer.parseInt(input.substring(4,6));
            int days = Integer.parseInt(input.substring(6,8));
            
            if( month<1 || month>12) {
                bw.write("#"+(i+1)+" -1\n");
                continue;
            }
            
            if(days==0 || days> daysInMonth[month-1]) {
                bw.write("#"+(i+1)+" -1\n");
                continue;
            }
            
            bw.write("#"+(i+1)
                    +" "+input.substring(0,4)
                    +"/"+input.substring(4,6)
                    +"/"+input.substring(6,8)+"\n");
        }
        bw.flush();
        
    }
    
    
}
 
cs