본문 바로가기

프로그래머스 - 코딩테스트

[Java][프로그래머스] - LVL.0 아이스 아메리카노


정답 1 - mine

이 문제를 처음 딱 봤을때 들었던 생각은 int 배열에 목과 나머지를 계산해서 때려 넣자! 였다.

그리하여 아래와 같이 작성해보았다.

class Solution {
    public int[] solution(int money) {
        int priceOfCoffee = 5500;
        int affortAmount = money / priceOfCoffee;
        int change = money % priceOfCoffee;
        int[] answer = {affortAmount, change};
        return answer;
    }
}

결과는 작동 됐다.

 

하지만 코드가 너무 길어보였다. 그래서 고민끝에 아래 정답 2 와같이 리펙토링을 해보았다.

 


정답 2 - mine

class Solution {
    public int[] solution(int money) {
        int[] answer = {money / 5500, money % 5500};
        return answer;
    }
}

훨씬 간결하고 깔끔해졌다.

 

이후 다른사람들은 어떻게 했을지가 궁금했다.

 


정답 3

class Solution {
    public int[] solution(int money) {
        return new int[] { money / 5500, money % 5500 };
    }
}

더 간결하고 깔끔한 답이 있었다. 저렇게 int 배열을 바로 초기화 해버리는 방법도 있었다.