JAVA 코딩 알고리즘 연습/프로그래머스
프로그래머스 - 부족한 금액 계산하기 / JAVA (자바) 코딩 알고리즘 연습
easpop
2022. 12. 24. 21:30
728x90
반응형
반응형
간단한 문제였다~~
total을 구하기 위해, price 금액을 count 개수만큼 누적해서 커지는 금액을 구한다
그래서 가지고 있는 money가 클 경우 0를 리턴하고, 아닐 경우는 부족한 금액을 리턴해준다!
class Solution {
public long solution(int price, int money, int count) {
long answer = 0;
long num = 0;
long total = price;
for (int i = 1; i < count; i++) {
num += price;
total += num + price;
}
if (money >= total) {
answer = 0;
} else {
answer = total - money;
}
return answer;
}
}
728x90
반응형