티스토리 뷰

728x90

 

생일케익의 초 숫자를 구하는 문제로,

4개의 캔들의 높이가 각각 4,4,1,3, 캔들이 주어졌을 때, 가장 높이가 큰 캔들의 숫자를 리턴하는 문제입니다.

 

1. 배열에서 가장 큰 캔들을 찾았고,

2. 가장 큰 캔들의 갯수를 찾는 구문을 각각 for 문으로 구현하였습니다.

 

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    // Complete the birthdayCakeCandles function below.
    static int birthdayCakeCandles(int[] ar) {

        int startnum = 0;
        int endnum = ar.length;
        int candlenum = ar[0];
        int cntcancle = 0;

        for(int i=startnum; i<endnum; i++){

            if(candlenum < ar[i]){
                candlenum = ar[i];
            }

        }

        for (int i=startnum; i<endnum; i++){

            if(candlenum==ar[i]){
               cntcancle = cntcancle + 1;
            }
        }

       return cntcancle;
    }

    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 = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        int[] ar = new int[arCount];

        String[] arItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        for (int i = 0; i < arCount; i++) {
            int arItem = Integer.parseInt(arItems[i]);
            ar[i] = arItem;
        }

        int result = birthdayCakeCandles(ar);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedWriter.close();

        scanner.close();
    }
}

728x90

'Java 코딩테스트' 카테고리의 다른 글

[hackerrank] Grading Students  (0) 2020.03.03
[hackerrank] plus minus  (0) 2020.02.28
[hackerrank] Diagonal Difference  (0) 2020.02.26
[hackerrank] min max sum  (0) 2020.02.25
coding practice for KaKao  (0) 2020.02.25
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함