본문 바로가기

문제풀이/C 문제풀이

[HackerRank]Mini-Max Sum

문제

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.
For example, arr = [1, 3, 5, 7, 9]. Our minimum sum is 1+3+5+7=16 and our maximum sum is 3+5+7+9=24. We would print 16 24.
Function Description
Complete the miniMaxSum function in the editor below. It should print two space-separated integers on one line: the minimum sum and the maximum sum of 4 of 5 elements.
Input Format
A single line of five space-separated integers.
Output Format
Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than a 32 bit integer.)

CODE

void miniMaxSum(int arr_count, int* arr) {
    unsigned long long int min = *arr, max = 0, sum = 0;
    for (int i;i<arr_count;i++){
        sum+=*(arr+i); // 합계
        if (min>*(arr+i)) min=*(arr+i); // 최솟값 구하기
        else if (max<*(arr+i)) max=*(arr+i); // 최댓값 구하기
    }
    printf("%llu %llu", sum-max, sum-min); // 최소합계, 최대합계 출력
}

감상

처음에 문제가 안 풀리길래 내가 hint로 주어진 "Beware of integer overflow! Use 64-bit Integer." 조건을 만족시키지 못한 건가 싶었다. 알고보니 내가 문제 조건인 minimum sum과 maximum sum을 만족시키지 못한 것이었다. 문제 잘 읽자
+) 64-bit inteager

'문제풀이 > C 문제풀이' 카테고리의 다른 글

[HackerRank]Time Conversion  (0) 2019.06.27
[HackerRank]Birthday Cake Candles  (0) 2019.06.26
[HackerRank]Staircase  (0) 2019.05.26
[HackerRank]Plus Minus  (0) 2019.05.24
[HackerRank]Diagonal Difference  (0) 2019.05.21