반응형
문제
- 문제 링크: 연월일 달력
풀이
(C++)
solution 1)
- 시간 복잡도:
더보기
더보기
#include<iostream>
#include<string>
using namespace std;
bool is_valid(string month, string day) {
int m = stoi(month);
int d = stoi(day);
if ((m <= 0) || (m > 12))
return false;
if (m == 2) {
if ((d <= 0) || (d > 28))
return false;
}
switch(m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if ((d <=0) || (d > 31))
return false;
case 4:
case 6:
case 9:
case 11:
if ((d <=0) || (d > 30))
return false;
default:
break;
}
return true;
}
int main(int argc, char** argv)
{
int test_case;
int T;
cin>>T;
for(test_case = 1; test_case <= T; ++test_case)
{
string text;
cin >> text;
string year, month, day;
year = text.substr(0,4);
month = text.substr(4,2);
day = text.substr(6,2);
bool check;
check = is_valid(month, day);
if (check)
cout << "#" << test_case << " " << year << "/" << month << "/" << day << endl;
else
cout << "#" << test_case << " " << -1 << endl;
}
return 0;
}
(Java)
solution 1)
- 시간 복잡도:
(Python)
solution 1)
- 시간 복잡도:
더보기
더보기
import sys
def is_valid(month, day):
m = int(month)
d = int(day)
if ((m <= 0) or (m > 12)):
return False
if (m == 2):
if ((d <= 0) or (d > 28)):
return False
if m in [1, 3, 5, 7, 8, 10, 12]:
if ((d <=0) or (d > 31)):
return False
elif m in [4, 6, 9, 11]:
if ((d <=0) or (d > 30)):
return False
return True
T = int(input())
for test_case in range(1, T + 1):
text = input()
year = text[0:4]
month = text[4:6]
day = text[6:]
check = is_valid(month, day)
if (check):
print(f"#{test_case} {year}/{month}/{day} ")
else:
print(f"#{test_case} -1")
반응형
'2-2. 코딩테스트 문제집(SW Expert Academy) > SWEA(D1)' 카테고리의 다른 글
[SWEA] D1: 홀수만 더하기(2072) 해설 (0) | 2025.01.31 |
---|---|
[SWEA] D1: 평균값 구하기(2071) 해설 (0) | 2025.01.31 |
[SWEA] D1: 큰 놈, 작은 놈, 같은 놈(2070) 해설 (0) | 2025.01.31 |
[SWEA] D1: 최대수 구하기(2068) 해설 (0) | 2025.01.12 |
[SWEA] D1: 중간값 찾기(2063) 해설 (0) | 2025.01.12 |