Archive/Code_HackerRank 2018. 1. 17. 00:20

[HackerRank]Simple Array Sum

Given an array of  integers, can you find the sum of its elements?

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.

Sample Input

6
1 2 3 4 10 11

Sample Output

31

Explanation

We print the sum of the array's elements, which is: .



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
 
using namespace std;
 
int simpleArraySum(int n, vector <int> ar) {
    // Complete this function
    int sum = 0;
    for(int i=0; i<n; i++){
        sum +=ar[i];
    }
    return sum;
}
 
int main() {
    int n;
    cin >> n;
    vector<int> ar(n);
    for(int ar_i = 0; ar_i < n; ar_i++){
       cin >> ar[ar_i];
    }
    int result = simpleArraySum(n, ar);
    cout << result << endl;
    return 0;
}
cs



Comment

vector<int> 를 매개인자로 주어진 모든 값을 받아왔기 때문에, vector<int> 안에 들어간 모든 값을 더해주기만 하면 끝.

'Archive > Code_HackerRank' 카테고리의 다른 글

[HackerRank]Plus Minus  (0) 2018.01.17
[HackerRank]Diagonal Difference  (0) 2018.01.17
[HackerRank]A Very Big Sum  (0) 2018.01.17
[HackerRank]Compare the Triplets  (0) 2018.01.17
[HackerRank]Solve Me First  (0) 2018.01.17