Post

[Programmers] #120820 - 나이 출력 [Java][C++][Python]

[Programmers] #120820 - 나이 출력 [Java][C++][Python]

문제 링크


1. 아이디어

2022년 기준 나이로 출생 연도를 구하는 문제다. 나이는 태어난 해에 1살이고 매년 1월 1일에 늘어나므로, 나이를 $a$라 하면 태어난 해로부터 $a - 1$년이 지난 것이다. 따라서 출생 연도는 $2022 - (a - 1) = 2023 - a$이다.


2. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
class Solution {
    public int solution(int age) {
        return 2023 - age;
    }
}
1
2
3
4
5
6
#include <bits/stdc++.h>
using namespace std;

int solution(int age) {
    return 2023 - age;
}
1
2
def solution(age):
    return 2023 - age

This post is licensed under CC BY 4.0 by the author.