부족한 금액 계산하기
📃 문제
🔨 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Solution {
public long solution(int price, int money, int count) {
long totalCost = 0;
for (int i = 1; i <= count; i++) {
totalCost += price * i;
}
long shortage = totalCost - money;
return shortage > 0 ? shortage : 0;
}
}
price: 놀이기구의 원래 이용료
money: 현재 자신이 가지고 있는 금액
count: 놀이기구를 탄 횟수
놀이기구를 탄 횟수만큼 총 요금을 계산
현재 가지고 있는 돈에서 총 요금을 뺀 값이 shortage
이값이 0보다 작다면 금액이 부족한 것이므로 부족한 금액을 반환하고
그렇지 않으면 0을 반환
This post is licensed under CC BY 4.0 by the author.