Chapter1 D-4 TIL

홍유택·2025년 1월 3일

나이출력하기

나의 답 :

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(int age) {
int answer = 2022 - age + 1
return answer;
}

다시 한번 생각해본 답:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(int age) {
int answer = 0;
int year = 2022;
int birth = year+1-age;
answer = birth;
return answer;
}

식을 구성할 때 직관적인 표현을 사용해서 함수의 값을 정의해줄 필요가 있는 것 같다.
첫번째처럼 현재가 2022라고 해서 바로 2022를 입력할 것 이 아니라 year이라는 칸을 만들고, birth를 구하는 식을 만들어 하나의 식을 만들어 답을 도출하는 것이 좋은 것 같다.

profile
안녕하세요

0개의 댓글