(Python, Java) 프로그래머스 - 부족한 금액 계산하기

[문제 링크]

Python 풀이

def solution(price, money, count):
    answer = 0

    for i in range(1, count + 1):
        answer += price * i

    return 0 if answer <= money else answer - money

Java 풀이

class Solution {
    public long solution(int price, int money, int count) {
        long answer = 0;
        long tot = 0;
        for (int i = 1; i <= count; i++) {
            tot += price * i;
        }

        if (tot > money) {
            answer = tot - money;
        }

        return answer;
    }
}

© 2021. By Backtony