알고리즘, 코딩테스트/알고리즘 풀이

[프로그래머스/ 코딩테스트 입문] 양꼬치

jiyoon0000 2025. 3. 6. 21:07

Q. 양꼬치

A1.

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

 

A2. 객체지향으로 풀어보기

// 가게와 관련된 정보 저장 class
class Store {

    // 양꼬치와 음료 가격
    private static final int lamb = 12000;
    private static final int drink = 2000;
    
    // 총 가격 계산 메서드
    // 양꼬치 10인분 당 음료 1개 제공
    public int totalPrice(int n, int k) {
        int freeDrink = n / 10;
        return (n * lamb) + ((k - freeDrink) * drink);
    }
}


class Solution {
    public int solution(int n, int k) {
    
        // Store 객체 생성 후 totalPrice 메서드 호출하여 가격 계산
        Store store = new Store();
        return store.totalPrice(n,k);
    }
}