본문 바로가기
Portpolio/codingtest

프로그래머스 옷가게 할인 받기 풀이

by Peter Choi 2025. 2. 3.
반응형

 

https://school.programmers.co.kr/learn/courses/30/lessons/120818

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

자바

class Solution {
    public int solution(int price) {
        int answer = 0;
        
        if (price<100000)
            answer = price;
        else if (100000<=price && price<300000)
            answer = price * 95/100;
        else if (300000<=price && price<500000)
            answer = price * 90/100;
        else
            answer = price * 80/100;
        
        return answer;
    }
}

 

 

자바스크립트

function solution(price) {
    var answer = 0;
    
    if (price<100000)
        answer = price;
    else if (price<300000)
        answer = Math.floor(price * 0.95);
    else if (price<500000)
        answer = Math.floor(price * 0.9);
    else
        answer = Math.floor(price * 0.8);
    
    return answer;
}

자바스크립트는 부동소수점 이슈가 존재하므로 위의 문제에서 소수점을 버려내는 과정이 필요합니다.

반응형

댓글