[프로그래머스]부족한 금액 계산하기

allnight5·2023년 1월 10일
0

프로그래머스

목록 보기
12/73

문제 설명
새로 생긴 놀이기구는 인기가 매우 많아 줄이 끊이질 않습니다. 이 놀이기구의 원래 이용료는 price원 인데, 놀이기구를 N 번 째 이용한다면 원래 이용료의 N배를 받기로 하였습니다. 즉, 처음 이용료가 100이었다면 2번째에는 200, 3번째에는 300으로 요금이 인상됩니다.
놀이기구를 count번 타게 되면 현재 자신이 가지고 있는 금액에서 얼마가 모자라는지를 return 하도록 solution 함수를 완성하세요.
단, 금액이 부족하지 않으면 0을 return 하세요.

제한사항
놀이기구의 이용료 price : 1 ≤ price ≤ 2,500, price는 자연수
처음 가지고 있던 금액 money : 1 ≤ money ≤ 1,000,000,000, money는 자연수
놀이기구의 이용 횟수 count : 1 ≤ count ≤ 2,500, count는 자연수

파이썬

def solution(price, money, count):
    answer = money - price*(1+count)*count//2
    if answer >0 :
        return 0 
    return -answer
def solution(price, money, count):
    answer = price*(1+count)*count//2-money 
    if answer <0:
        return 0
    return answer

이것을 for문을 이용해서 풀기싫어.. 검색하면서 등비수열인지 등차수열인지도 헷갈려서.. 계산식을 찾아다가 적었다.. 다음에는 내가 계산식을 구하고 싶기도 한데.. 이미 있는걸 찾아쓰는게 빠를지도..

자바 첫번째 실패

class Solution {
    public long solution(int price, int money, int count) {
        return Math.max(money - price * (count * (count + 1) / 2), 0);
    }
}

자바 세번째 성공

class Solution {
    public long solution(int price, int money, int count) {
        long answer = -1;
        answer = (long)price*count*(count+1)/2 - money;
        return answer<=0?0:answer;
    }
}

자바 형변환 으로 인한 오류 화난다..

int price를 long price로 바꿔주면된다.

class Solution {
    public long solution(long price, int money, int count) {
        return Math.max(0, price * (count * (count + 1) / 2) - money);
    }
}
profile
공부기록하기

0개의 댓글