본문 바로가기

문제풀이/C 문제풀이

[HackerRank]Birthday Cake Candles

You are in charge of the cake for your niece's birthday and have decided the cake will have one candle for each year of her total age. When she blows out the candles, she’ll only be able to blow out the tallest ones. Your task is to find out how many candles she can successfully blow out.
Function Description
Complete the function birthdayCakeCandles in the editor below. It must return an integer representing the number of candles she can blow out.
Input Format
The first line contains a single integer, n, denoting the number of candles on the cake.
The second line contains n space-separated integers, where each integer i describes the height of candle i.
Output Format
Print the number of candles that can be blown out on a new line.

CODE

int birthdayCakeCandles(int ar_count, int* ar) {
    int count, m = 0;
    for (int i = 0;i<ar_count;i++) {
        if (*(ar+i)>m) { // 최대 높이 값이 변한다면
            count = 1; // count 초기화
            m=*(ar+i); // 최댓값 초기화
        }
        else if (*(ar+i)==m) count++; // 최대 높이 값과 같다면 count 증가
    }
    return count;
}

감상

어떻게 하면 더 간단한 코드를 만들 수 있을지 고민이다

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

[HackerRank]Grading Students  (0) 2019.07.01
[HackerRank]Time Conversion  (0) 2019.06.27
[HackerRank]Mini-Max Sum  (0) 2019.06.25
[HackerRank]Staircase  (0) 2019.05.26
[HackerRank]Plus Minus  (0) 2019.05.24