Archive/Code_HackerRank

[HackerRank]A Very Big Sum

호리동 2018. 1. 17. 00:30

You are given an array of integers of size . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large.

Input Format

The first line of the input consists of an integer . The next line contains  space-separated integers contained in the array.

Output Format

Print a single value equal to the sum of the elements in the array.

Constraints 
 

Sample Input

5
1000000001 1000000002 1000000003 1000000004 1000000005

Output

5000000015

Note:

The range of the 32-bit integer is .

When we add several integer values, the resulting sum might exceed the above range. You might need to use long long int in C/C++ or long data type in Java to store such sums.


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



Commnet

long int 타입의 변수. 이전에 문제를 풀다보니까 long int 혹은 long long int 타입의 변수를 이용해야 풀 수 있는 문제들이 꽤 많이 나온다. int와 long int, long long int의 차이점을 확실히 알고 있고, 사용할 수 있어야 한다.