-
위클리 챌린지 1주차코딩 테스트/Level 1 2021. 8. 14. 14:52반응형
https://programmers.co.kr/learn/courses/30/lessons/82612
def solution(price, money, count): return 0 if (n := money - price * (1 + count) * count / 2) > 0 else -n
def solution(price, money, count): return 0 if (n := price * (1 + count) * count / 2 - money) < 0 else n
def solution(price, money, count): return max(price * (1 + count) * count / 2 - money, 0)
golang
func solution(price int, money int, count int) int64 { n := money - price*(count+1)*count/2 if n >= 0 { return 0 } return -int64(n) }
Java
class Solution { public long solution(int price, int money, int count) { var leftMoney = (long) money; for (var i = 1; i <= count; i++) leftMoney -= price * i; return leftMoney < 0 ? -leftMoney : 0; } }
실수 범위에 대한 문제입니다.
class Solution { public long solution(int price, int money, int count) { var leftMoney = money - (long) price * (count + 1) * count / 2; return leftMoney < 0 ? -leftMoney : 0; } }
(long)을 이용해 명시적인 형변환을 했습니다.
class Solution { public long solution(int price, int money, int count) { return Math.max((long) price * (count + 1) * count / 2 - money, 0); } }
반응형