문제
정수 리스트 num_list가 주어질 때, 마지막 원소가 그전 원소보다 크면 마지막 원소에서 그전 원소를 뺀 값을 마지막 원소가 그전 원소보다 크지 않다면 마지막 원소를 두 배한 값을 추가하여 return하도록 solution 함수를 완성해주세요.
솔루션
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
// num_list_len은 배열 num_list의 길이입니다.
int* solution(int num_list[], size_t num_list_len) {
// return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
int* answer = (int*)malloc((num_list_len+1) * sizeof(int));
for(int i = 0; i < num_list_len; i++)
answer[i] = num_list[i];
if(num_list[num_list_len-1] > num_list[num_list_len - 2])
answer[num_list_len] = num_list[num_list_len-1] - num_list[num_list_len-2];
else
answer[num_list_len] = num_list[num_list_len-1]*2;
return answer;
}
https://school.programmers.co.kr/learn/courses/30/lessons/181927
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
'프로그래머스코딩테스트연습풀이 > C언어' 카테고리의 다른 글
[프로그래머스/C언어] 카운트 업 (0) | 2023.07.31 |
---|---|
[프로그래머스/C언어] 수 조작하기 1 (0) | 2023.07.31 |
[프로그래머스/C언어] 이어 붙인 수 (0) | 2023.07.31 |
[프로그래머스/C언어] 원소들의 곱과 합 (0) | 2023.07.31 |
[프로그래머스/C언어] flag에 따라 다른 값 반환하기 (0) | 2023.07.31 |