본문 바로가기

문제풀이/C 문제풀이

[HackerRank]Simple Array Sum

문제

Given an array of integers, find the sum of its elements.

Function Description
Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.
simpleArraySum has the following parameter(s):
ar: an array of integers
Input Format
The first line contains an integer, , denoting the size of the array.
The second line contains space-separated integers representing the array's elements.
Output Format
Print the sum of the array's elements as a single integer.

CODE

int simpleArraySum(int ar_count, int* ar) { // 숫자, 배열을 입력 받으면
int i, sum=0; // i : for문의 index, sum : 합
for (i=0;i<ar_count;i++) sum+=ar[i]; // 배열의 원소의 합을 구한다
return sum; // 합을 return한다
}


감상

실제로 문제를 풀면 전체적으로 매우 긴 코드가 있어서 당황했다. 실제로 작성해야하는 코드만 작성하면 나머지는 알아서 실행된다. 포인터를 잘 이해하고 있다면 어려움 없이 넘어갈 수 있다

'문제풀이 > C 문제풀이' 카테고리의 다른 글

[Hackerrank]A Very Big Sum  (0) 2019.05.20
[Hackerrank]Compare the Triplets  (0) 2019.05.17
1181  (0) 2019.03.01
1427  (0) 2019.03.01
2108  (0) 2019.03.01