본문 바로가기
algorithm/hackerRank

hackerrank A Very BigSum

by 무대포 개발자 2020. 9. 13.
728x90
반응형

hackerrank aVeryBigSum

문제

풀이

  • 큰수가 나올 수 있으니 BigInteger 사용
  • 테스트 케이스 아래에 첨
package hackerrank;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class AVeryBigSum {
    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(in.readLine());
        BigInteger[] bigIntegers = new BigInteger[num];
        String[] str = in.readLine().split(" ");

        for (int i = 0 ; i < str.length ; i++) {
            bigIntegers[i] = new BigInteger(str[i]);
        }

        BigInteger result = aVeryBigSum(bigIntegers);
        System.out.println(result);
    }

    public static BigInteger aVeryBigSum(BigInteger[] bigIntegers) {
        BigInteger sum = BigInteger.ZERO;
        for (BigInteger bigInteger : bigIntegers) {
            sum = sum.add(bigInteger);
        }
        return sum;
    }
}





public class AVeryBigSumTest {

    @Test
    public void aVeryBigSumTest() {
        BigInteger[] bigIntegers = new BigInteger[3];
        bigIntegers[0] = new BigInteger("100");
        bigIntegers[1] = new BigInteger("200");
        bigIntegers[2] = new BigInteger("300");
        Assert.assertThat(AVeryBigSum.solve(bigIntegers), is(new BigInteger("600")));
    }
}

'algorithm > hackerRank' 카테고리의 다른 글

hackerrank the-time-in-words 풀이  (0) 2020.09.18
Hackerrank Bigger is greater  (0) 2020.09.14
[HackerRank] Encryption  (0) 2020.07.10
[HackerRank] Climbing the Leaderboard  (0) 2020.07.10
[HackkerRank] Sherlock and Anagrams  (0) 2018.10.12

댓글