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.
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static int simpleArraySum(int n, int[] ar) { // Complete this function int result = 0; for(int i=0; i<n; i++){ result += ar[i]; } return result; } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] ar = new int[n]; for(int ar_i = 0; ar_i < n; ar_i++){ ar[ar_i] = in.nextInt(); } int result = simpleArraySum(n, ar); System.out.println(result); } }
'Programming > >> Algorithm' 카테고리의 다른 글
[Lucky Algorithm] A Very Big Sum (0) | 2017.10.03 |
---|---|
[Lucky Algorithm] Compare the Triplets (0) | 2017.10.03 |
[Lucky Algorithm] Mini-Max Sum (0) | 2017.10.03 |
[Lucky Algorithm] Grading Students (0) | 2017.09.27 |
[Lucky Algorithm] 시작 (0) | 2017.09.27 |
댓글