반응형
Integer Array를 받아 개별요소들의 합을 구하는 문제.
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
int arCount = Integer.parseInt(scanner.nextLine().trim());
int[] ar = new int[arCount];
String[] arItems = scanner.nextLine().split(" ");
for (int arItr = 0; arItr < arCount; arItr++) {
int arItem = Integer.parseInt(arItems[arItr].trim());
ar[arItr] = arItem;
}
int result = simpleArraySum(ar);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
}
/*
* Complete the simpleArraySum function below.
*/
static int simpleArraySum(int[] ar) {
int sum = 0;
for (int element : ar) {
sum += element;
}
return sum;
}
}
입력받은 Integer Array를 for문을 돌며 더하여 return 하면 된다.
문제 출처 : www.hackerrank.com
반응형
'Algorithm' 카테고리의 다른 글
A Very Big Sum (0) | 2020.10.10 |
---|---|
Compare the Trrplets (0) | 2020.10.10 |
K번째 (0) | 2020.08.22 |
더 맵게 (0) | 2020.08.22 |
주식가격 (0) | 2020.08.21 |