728x90
문제
내풀이
필요없는 것을 많이 한 것 같다.
예를들어 int로 형변환?
class Solution {
public int solution(int n, int k) {
int answer = 0;
int sheep = 12000;
int drink = 2000;
int service = 0;
service = (int) ((n / 10) * drink);
answer = (n*sheep) + (k*drink) - service;
return answer;
}
}
다른 사람 풀이
#1 간결해서 좋다
class Solution {
public int solution(int n, int k) {
return n * 12000 + k * 2000 - (n / 10 * 2000);
}
}
#2 객체지향을 활용한 답안
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;
}
}
⭐ 객체로 변환하는 방법에 익숙해질 필요가 있다.
'알고리즘 > SQL 풀이' 카테고리의 다른 글
프로그래머스| 0단계 문자열 뒤집기 (0) | 2023.06.14 |
---|---|
프로그래머스| 0단계 배열 자르기 (0) | 2023.06.04 |
프로그래머스| 0단계 배열 원소의 길이 (0) | 2023.06.04 |
프로그래머스| 0단계 배열의 평균 (0) | 2023.06.03 |
프로그래머스| 레벨0 각도기 (0) | 2023.06.03 |