양꼬치

반즈·2023년 11월 13일

프로그래머스 입문

목록 보기
3/51

문제 설명

머쓱이네 양꼬치 가게는 10인분을 먹으면 음료수 하나를 서비스로 줍니다. 양꼬치는 1인분에 12,000원, 음료수는 2,000원입니다. 정수 n과 k가 매개변수로 주어졌을 때, 양꼬치 n인분과 음료수 k개를 먹었다면 총얼마를 지불해야 하는지 return 하도록 solution 함수를 완성해보세요.

입출력 예

자바

나의 풀이

class Solution {
    public int solution(int n, int k) {
        int answer = 0;
        int tempValue = 0;
        if(n >= 10){
            tempValue = n / 10;
        }
        answer = (12000 * n) + (2000 * (k-tempValue));
        return answer;
    }
}

참고 풀이 1 (객체지향)

class Solution {
    public int solution(int n, int k) {
        int lambTotalPrice = totalPrice(Menu.LAMB, n);
        int drinkTotalPrice = totalPrice(Menu.DRINK, k);
        int discountPrice = discount(Menu.DRINK, n);

        int totalPay = lambTotalPrice + drinkTotalPrice - discountPrice;
        return totalPay;
    }

    private int totalPrice(Menu menu, int quantity) {
     return menu.getPrice() * quantity;   
    }

    private int discount(Menu menu, int lambQuantity) {
        // 양꼬치 10인분에 음료수 하나
        int point = lambQuantity / 10;

        return menu.getPrice() * point;
    }
}

enum Menu {

    LAMB("양꼬치", 12000),
    DRINK("음료수", 2000);

    private final String name;
    private final int price;

    Menu(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }
}

참고 풀이 2 (간단)

class Solution {
    public int solution(int n, int k) {
        return n * 12000 + k * 2000 - (n / 10 * 2000);
    }
}

자바스크립트

나의 풀이

function solution(n, k) {
    var answer = 0;
    var service = 0;
    if(n >= 10){
        service = Math.trunc(n / 10);
    }
    answer = (12000 * n) + (2000 * (k-service));
    return answer;
}

참고 풀이 1 (틸트 연산자)

function solution(n, k) {
    k-=~~(n/10);
    if (k < 0) k = 0;
    return n*12000+k*2000;
}

참고 풀이 2 (parseInt)

function solution(n, k) {
    return n*12000 + k*2000 - parseInt(n/10)*2000
}
profile
나를 채우다

0개의 댓글