프로그래머스코딩테스트연습풀이/C언어
[프로그래머스/C언어] 길이에 따른 연산
코코쵸마
2023. 8. 1. 11:47
문제
정수가 담긴 리스트 num_list가 주어질 때, 리스트의 길이가 11 이상이면 리스트에 있는 모든 원소의 합을 10 이하이면 모든 원소의 곱을 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) {
int answer = 0;
if(num_list_len >= 11)
for(int i = 0; i < num_list_len; i++)
answer += num_list[i];
else{
answer = 1;
for(int j = 0; j < num_list_len; j++)
answer *= num_list[j];
}
return answer;
}
https://school.programmers.co.kr/learn/courses/30/lessons/181879