정수 리스트 num_list가 주어질 때, 마지막 원소가 그전 원소보다 크면 마지막 원소에서 그전 원소를 뺀 값을 마지막 원소가 그전 원소보다 크지 않다면 마지막 원소를 두 배한 값을 추가하여 return하도록 solution 함수를 완성해주세요.
2 ≤ num_list의 길이 ≤ 10
1 ≤ num_list의 원소 ≤ 9
num_list result
[2, 1, 6] [2, 1, 6, 5]
[5, 2, 1, 7, 5] [5, 2, 1, 7, 5, 10]
입출력 예 #1
마지막 원소인 6이 그전 원소인 1보다 크기 때문에 6 - 1인 5를 추가해 return합니다.
입출력 예 #2
마지막 원소인 5가 그전 원소인 7보다 크지 않기 때문에 5의 두 배인 10을 추가해 return합니다.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
// num_list_len은 배열 num_list의 길이입니다.
int* solution(int num_list[], size_t num_list_len) {
int *answer = (int *)malloc(sizeof(int) * (num_list_len + 2));
int a = 0;
int b = 0;
for (int i = 0; i < num_list_len; i++) {
answer[i] = num_list[i];
if (i == num_list_len - 2)
a = num_list[i];
if (i == num_list_len - 1)
b = num_list[i];
}
if (a < b)
answer[num_list_len] = b - a;
else
answer[num_list_len] = 2 * b;
answer[num_list_len + 1] = '\0'; // 문자열배열이 아니므로 필요없었던 코드
return answer;
}