728x90
문제
* 년도는 2015년을 기준으로 계산
* 입력된 월, 일과 2월 18일을 비교
* 2월 18일과 같으면 Special 출력
* 2월 18일보다 빠르면 Before 출력
* 2월 18일보다 늦으면 After 출력
문제 링크 : https://www.acmicpc.net/problem/10768
LocalDateTime 사용 방법
/* 객체 생성 방법 */
LocalDateTime BeforeTime = LocalDateTime.of(년도, 월, 일, 시, 분, 초);
LocalDateTime AfterTime = LocalDateTime.of(년도, 월, 일, 시, 분, 초);
/* 비교 방법 */
BeforeTime.isBefore(AfterTime);
AfterTime.isAfter(BeforeTime);
SameTime.isEquals(SameTime);
풀이방법
1. 조건문 구현
2. LocalDateTime 라이브러리 사용
나의 코드
두 방법 모두 메모리와 실행속도의 차이 거의 없다.
[성공] 시도1. 조건문 구현
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int month = Integer.parseInt(br.readLine());
int day = Integer.parseInt(br.readLine());
if(month==2) {
if(day==18) System.out.println("Special");
else if(day>18) System.out.println("After");
else System.out.println("Before");
}
else if (month>2) System.out.println("After");
else System.out.println("Before");
}
2. LocalDateTime 라이브러리 사용
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int month = Integer.parseInt(br.readLine());
int day = Integer.parseInt(br.readLine());
LocalDateTime base = LocalDateTime.of(2015, 2, 18,0,0,0);
LocalDateTime input = LocalDateTime.of(2015, month, day,0,0,0);
if(base.isEqual(input)) System.out.println("Special");
else if (base.isAfter(input)) System.out.println("Before");
else System.out.println("After");
}
728x90
'알고리즘 저장소 (일반방식과 나만의 풀이) > JAVA' 카테고리의 다른 글
[백준] no14940: 쉬운 최단거리 (0) | 2023.06.06 |
---|---|
[백준] no11444: 피보나치 수 6 - 수학(행렬) (0) | 2023.06.05 |
[백준] no1967: 트리의 지름 (0) | 2023.06.02 |
[백준] no11404: 플로이드 - BFS + 우선순위큐 (0) | 2023.06.01 |
[백준] no9251: LCS - DP문제 (0) | 2023.05.30 |