[Algorithm/Python][SWEA] 2056번 연월일 달력

동글이·2022년 11월 13일
0

Algorithm

목록 보기
32/33

[SWEA] 2056. 연월일 달력

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=1&contestProbId=AV5QLkdKAz4DFAUq&categoryId=AV5QLkdKAz4DFAUq&categoryType=CODE&problemTitle=&orderBy=RECOMMEND_COUNT&selectCodeLang=ALL&select-1=1&pageSize=10&pageIndex=1

- 문제 접근

  • 문법 :
    1) 슬라이싱 : 문자열[시작문자인덱스:끝문자인덱스+1]
    month = int(num[4:6])
    2) .zfill() : 문자열.zfill(개수)
    result=str(year).zfill(4)+"/"+str(month).zfill(2)+"/"+str(day).zfill(2)

- 내 코드

T=int(input())

for t in range(T):
    result=""
    num=input()
    year=int(num[0:4])
    month = int(num[4:6])
    day = int(num[6:8])
    if month==1 or month==3 or month==5 or month==7 or month==8 or month==10 or month==12:
        if day>=1 and day<=31:
            result=str(year).zfill(4)+"/"+str(month).zfill(2)+"/"+str(day).zfill(2)
        else:
            result="-1"
    elif month==4 or month==6 or month==9 or month==11:
        if day>=1 and day<=30:
            result=str(year).zfill(4)+"/"+str(month).zfill(2)+"/"+str(day).zfill(2)
        else:
            result="-1"
    elif month==2:
        if day>=1 and day<=28:
            result=str(year).zfill(4)+"/"+str(month).zfill(2)+"/"+str(day).zfill(2)
        else:
            result="-1"
    else:
        result="-1"
    print("#{} {}".format(t+1,result))

- 더 멋진 코드

t=int(input())

for i in range(t):
    print('#%s' %(i+1),end=' ')
    date=input()
    year=int(date[:4])
    month=int(date[4:6])
    dd=int(date[6:])
    if month <1 or month>12:
        print(-1)
        continue

    if month in [1,3,5,7,8,10,12]:
        if dd <1 or dd>31:
            print(-1)
            continue
    if month ==2:
        if dd<1 or dd>28:
            print(-1)
            continue

    if month in [4,6,9,11]:
        if dd <1 or dd>30:
            print(-1)
            continue
    print("%04d/%02d/%02d" %(year,month,dd))
profile
기죽지 않는 개발자

0개의 댓글