본문 바로가기
알고리즘/과제

SW Expert Academy 2056. 연월일 달력

by Lihano 2021. 8. 14.
반응형

풀이 언어 : JAVA

import java.util.Scanner;
import java.io.FileInputStream;
 
class Solution
{
    public static void main(String args[]) throws Exception
    {
        /*
           표준입력 System.in 으로부터 스캐너를 만들어 데이터를 읽어옵니다.
         */
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();
        /*
           여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
        */
        for(int test_case = 1; test_case <= T; test_case++)
        {
            String input = sc.next();
             
            if (input.length() != 8) {
                System.out.println("#" + test_case + " " + "-1");
                continue;
            }
            
            String year_s = input.substring(0,4);
            String month_s = input.substring(4,6);
            String day_s = input.substring(6,8);
            
            int month = Integer.parseInt(month_s);
            int day = Integer.parseInt(day_s);
             
            if (month > 12 || month < 1) {
                System.out.println("#" + test_case + " " + "-1");
                continue;
            }
            
            if (month == 2) {
                if (day > 28) {
                    System.out.println("#" + test_case + " " + "-1");
                continue;
                }
            } else if (month == 1 || month == 3 || month == 5 || month == 7 || month ==8 || month == 10 || month ==12) {
                if (day > 31) {
                    System.out.println("#" + test_case + " " + "-1");
                continue;
                }
            } else {
                if (day > 30) {
                    System.out.println("#" + test_case + " " + "-1");
                    continue;
                }
            }
            
            System.out.println("#" + test_case + " " + year_s + "/" + month_s + "/" + day_s);
        }
        return;
    }
}

 

링크

SW Expert Academy

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

반응형

댓글