"SWEA 2056 연월일 달력"의 두 판 사이의 차이

61번째 줄: 61번째 줄:
k = int(input())
k = int(input())
md = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
md = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
for i in range(0, k):
for i in range(0, k):
     cal = input()
     cal = input()
66번째 줄: 67번째 줄:
     month = int(cal[4:6])
     month = int(cal[4:6])
     day = int(cal[6:8])
     day = int(cal[6:8])
    print("#%d " %(i+1), end = '')
     if(((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)):
     if(((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)):
         md[1] = 29
         md[1] = 29
     if month <= 0 or month >= 13:
     if month <= 0 or month >= 13:
        print("#%d " %(i+1), end = '')
         print(-1)
         print(-1)
     else:
     else:
         if (month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12):
         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):
             if(day>0 and day <32):
                print("#%d " %(i+1), end = '')
                 print("%.4d/%.2d/%.2d" %(year, month, day))
                 print("%.4d/%.2d/%.2d" %(year, month, day))
             else:
             else:
                print("#%d " %(i+1), end = '')
                 print(-1)
                 print(-1)
         else:
         else:
             if(month == 2):
             if(month == 2):
                 if (day>0 and day<=md[1]):
                 if (day>0 and day<=md[1]):
                    print("#%d " %(i+1), end = '')
                     print("%.4d/%.2d/%.2d" %(year, month, day))
                     print("%.4d/%.2d/%.2d" %(year, month, day))
                 else:
                 else:
                    print("#%d " %(i+1), end = '')
                     print(-1)
                     print(-1)
             else:
             else:
                 if(day>0 and day <31):
                 if(day>0 and day <31):
                    print("#%d " %(i+1), end = '')
                     print("%.4d/%.2d/%.2d" %(year, month, day))
                     print("%.4d/%.2d/%.2d" %(year, month, day))
                 else:
                 else:
                    print("#%d " %(i+1), end = '')
                     print(-1)
                     print(-1)
</source>
</source>

2019년 11월 25일 (월) 13:59 판

1 개요

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

틀:SWEA 난이도 1-1

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 }}