주니어 기초 코딩공부/JAVA_programmers_코딩테스트

옷가게 할인 받기_programmers_lev00

jju_developer 2022. 12. 2. 21:46
728x90

<코딩 테스트>

머쓱이네 옷가게는 10만 원 이상 사면 5%, 30만 원 이상 사면 10%, 50만 원 이상 사면 20%를 할인해줍니다.
구매한 옷의 가격 price가 주어질 때, 지불해야 할 금액을 return 하도록 solution 함수를 완성해보세요.

 

<나의 풀이 과정>

우선, 할인률 계산법을 생각해보았습니다.

이때 간단하게 할인율을 구하기 위해서

5% 할인률 일때 answer = (int)(price*0.95); 로 구현하였습니다.

 

<나의 풀이 코드>

    static int solution(int price) {
        double answer = 0;
        
        if(price>=500000) {
        	answer = (price*0.8);	
        }else if(price>=300000) {
        	answer = (price*0.9);
        }else if(price>=100000) {
        	answer = (price*0.95);	
        }
        return (int)answer;
    }
}

 

 

<다른사람 풀이>

class Solution {
    public int solution(int price) {
        int answer = 0;

        if(price>=500000) return (int)(price*0.8);
        if(price>=300000) return (int)(price*0.9);
        if(price>=100000) return (int)(price*0.95);

        return price;
    }
}
728x90