T = int(input())
for test_case in range(1, T + 1):
data = input()
year = data[0:4]
month = data[4:6]
day = data[6:8]
if (int(month) in [1, 3, 5, 7, 8, 10, 12]):
if int(day) > 31:
print("#" + str(test_case) + " -1")
else:
print("#" + str(test_case) + " "+ str(year)+"/"+str(month)+"/"+str(day))
elif (int(month) in [4, 6, 9, 11]):
if int(day) > 30:
print("#" + str(test_case) + " -1")
else:
print("#" + str(test_case) + " "+ str(year)+"/"+str(month)+"/"+str(day))
elif (int(month) == 2):
if int(day) > 29:
print("#" + str(test_case) + " -1")
else:
print("#" + str(test_case) + " "+ str(year)+"/"+str(month)+"/"+str(day))
else:
print("#" + str(test_case) + " -1")
딕셔너리를 쓰면 코드 길이가 줄어든다...
그리고 다시보니 이상하게 풀었는데 정답이어서 다시 고침.. 정신좀 차리고 풀자..
#수정한 코드
T = int(input())
for test_case in range(1, T + 1):
data = input()
year = data[0:4]
month = data[4:6]
day = data[6:8]
if (int(month) in [1, 3, 5, 7, 8, 10, 12]) and 0 < int(day) < 32:
print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
elif (int(month) in [4, 6, 9, 11]) and 0 < int(day) < 31:
print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
elif (int(month) == 2) and 0 < int(day) < 29:
print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
else:
print("#" + str(test_case) + " -1")
#딕셔너리 사용할 경우
...
day_check= {1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31}
if 0< int(month) < 13 and int(day) <= day_check[int(month)]:
print("#" + str(test_case) + " " + str(year) + "/" + str(month) + "/" + str(day))
else:
print("#" + str(test_case) + " -1")
'Algorithm > SWEA' 카테고리의 다른 글
[SWEA / python] 2047. 신문 헤드라인 (0) | 2023.05.11 |
---|---|
[SWEA / python] 2050. 알파벳을 숫자로 변환 (0) | 2023.05.11 |
[SWEA / python] 2058. 자릿수 더하기 (0) | 2023.05.10 |
[SWEA / python] 2063. 중간값 찾기 (0) | 2023.05.10 |
[SWEA / python] 2068. 최대수 구하기 (0) | 2023.05.10 |