SWEA 2056 연월일 달력

1 개요[ | ]

SWEA 2056 연월일 달력
  • SW Expert Academy 2056번 문제
  • SWEA D1
  • substring 메소드 등으로 자리수에 맞추어 월(month)과 일(day)을 구한다.
  • 월(month)이 1~12 사이인지 확인한다.
  • 일(day)이 해당월의 일자(예: 1~31)에 속하는지 확인한다.

2 C++[ | ]

#include <iostream>
#include <vector>
using namespace std;
int main() {
    vector<int> daysOfMonth = {31,28,31,30,31,30,31,31,30,31,30,31};
    int T;
    cin >> T;
    string s;
    for(int tc=1; tc<=T; tc++) {
        cin >> s;
        int month = atoi(s.substr(4,2).c_str());
        int day = atoi(s.substr(6).c_str());
        cout << "#" << tc << " ";
        if( 1<=month && month<=12 && 1<=day && day<=daysOfMonth[month-1] ) {
            cout << s.substr(0,4) << "/" << s.substr(4,2) << "/" << s.substr(6) << endl;
        }
        else {
            cout << -1 << endl;
        }
    }
}

3 Java[ | ]

import java.util.Scanner;
class Solution {
    public static void main(String args[]) {
        int daysOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int tc=1; tc<=T; tc++) {
            String s = sc.next();
            int month = Integer.valueOf(s.substring(4,6));
            int day = Integer.valueOf(s.substring(6,8));
            String res = "-1";
            if( 1<=month && month<=12 && 1<=day && day<=daysOfMonth[month-1] ) {
                res = String.format("%s/%s/%s", s.substring(0,4), s.substring(4,6), s.substring(6,8));
            }
            System.out.format("#%d %s\n", tc, res);
        }
    }
}

4 Python[ | ]

#kcy
k = int(input())
md = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

for i in range(0, k):
    cal = input()
    year = int(cal[0:4])
    month = int(cal[4:6])
    day = int(cal[6:8])

    print("#%d " %(i+1), end = '')

    if(((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)):
        md[1] = 29
    if month <= 0 or month >= 13:
        print(-1)
    else:
        if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
            if(day>0 and day <32):
                print("%.4d/%.2d/%.2d" %(year, month, day))
            else:
                print(-1)
        else:
            if(month == 2):
                if (day>0 and day<=md[1]):
                    print("%.4d/%.2d/%.2d" %(year, month, day))
                else:
                    print(-1)
            else:
                if(day>0 and day <31):
                    print("%.4d/%.2d/%.2d" %(year, month, day))
                else:
                    print(-1)
md = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
T = int(input())
for tt in range(T):
    s = input()
    month = int(s[4:6])
    day = int(s[6:8])
    res = "-1"
    if 1<=month and month<=12 and 1<=day and day<=md[month-1]:
    	res = s[0:4]+"/"+s[4:6]+"/"+s[6:8]
    print( f"#{tt+1} {res}" )
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}