-
[백준] 16199 나이 계산하기_Java코딩테스트 2024. 5. 1. 18:50
한국에서 나이는 총 3가지 종류가 있다.
문제 링크
https://www.acmicpc.net/problem/16199
문제 풀이
1. 만 나이, 세는 나이, 연 나이를 구하여 출력하라.
만 나이 : 생일을 기준으로 하여, 태어난 후 0살부터 생일이 지날 때마다 1살씩 증가시켜 계산한다.
세는 나이 : 연도를 기준으로 하여, 태어난 후 1살부터 연도가 바뀔 때 마다 1살씩 증가시켜 계산한다.
연 나이: 연도를 기준으로 하여, 태어난 후 0살부터 연도가 바뀔 때 마다 1살씩 증가시켜 계산한다.
2. 입력 값코드1
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(bf.readLine()); int[] birthday = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; st = new StringTokenizer(bf.readLine()); int[] standardDay = new int[]{Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; int dis = standardDay[0] - birthday[0]; int age1 = birthday[1] > standardDay[1] || (birthday[1] == standardDay[1] && birthday[2] > standardDay[2]) ? dis-1 : dis; int age2 = dis+1; int age3 = dis; System.out.println(age1 +"\n"+age2 +"\n"+age3); } }
실행 결과1
코드2_LocalDate를 이용
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.time.LocalDate; public class Main { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] birthday = bf.readLine().split(" "); String[] standardDay = bf.readLine().split(" "); LocalDate birthDate = LocalDate.of(Integer.parseInt(birthday[0]),Integer.parseInt(birthday[1]),Integer.parseInt(birthday[2])); LocalDate standardDate = LocalDate.of(Integer.parseInt(standardDay[0]),Integer.parseInt(standardDay[1]),Integer.parseInt(standardDay[2])); int dis = standardDate.getYear() - birthDate.getYear() ; LocalDate tempDate = birthDate.plusYears(dis); int age1 = standardDate.isBefore(tempDate)? dis-1 : dis; int age2 = dis+1; int age3 = dis; System.out.println(age1 +"\n"+age2 +"\n"+age3); } }
실행 결과2
'코딩테스트' 카테고리의 다른 글
[백준] 11659 구간 합 구하기 4_Java (1) 2024.05.01 [백준] 5532 방학 숙제_Java (1) 2024.05.01 [프로그래머스] 할인 행사_Java (0) 2024.04.29 [프로그래머스] 괄호 회전하기_Java (0) 2024.04.26 [백준] 30970 선택의 기로_Java (0) 2024.04.24