[알고리즘문제풀기] 옷가게 할인 받기

silver's avatar
Dec 13, 2024
[알고리즘문제풀기] 옷가게 할인 받기
Contents
문제

문제

내가 작성한 오답

: answer을 리턴하는데 answer이 0으로 초기화 되어있어 10만원이하에서는 0이 리턴이 됐다.
class Solution { public int solution(int price) { int answer = 0; if(price>=500000) answer = (int)(price*0.8); else if(price>=300000) answer = (int)(price*0.9); else if(price>=100000) answer = (int)(price*0.95); return answer; } }

내가 작성한 정답

class Solution { public int solution(int price) { int answer = price; if(price>=500000) answer = (int)(price*0.8); else if(price>=300000) answer = (int)(price*0.9); else if(price>=100000) answer = (int)(price*0.95); return answer; } }
 

다른 사람들의 정답

: 삼항연산자 이용
class Solution { public int solution(int price) { int answer = 0; double ratio=((price>=500000)?(0.8):((price>=300000)?(0.9):((price>=100000)?(0.95):(1.0)))); answer = (int)(price*ratio); return answer; } }
💡
조건? 조건 만족 시 return값 : 조건 만족하지 못할 경우 - 출력될 값 또는 조건을 걸어서 삼항연산자 이어갈 수 있음
 
Share article

silver