def solution(price, money, count):
m = [x * price for x in range(1, count + 1)]
answer = money - sum(m)
if answer < 0: #단, 금액이 부족하지 않으면 0을 return 하세요.
return -answer
else:
return 0
function solution(price, money, count) {
//Array.from({length: 5}, (v, i) => i);
// [0, 1, 2, 3, 4]
const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));
let list = range(1, count, 1);
list = list.map((num) => num * price);
let s = list.reduce( (a, b) => a + b);
let answer = money - s;
if (answer < 0)
return -answer;
else
return 0;
}
🛑 C, C++에서는 변수 선언 타입 잘 정해야한다!!!
#include <iostream>
using namespace std;
long long solution(int price, int money, int count)
{
long long sum = 0;
for(int i = 1; i <= count; i++){
sum += i * price;
}
long long answer = money - sum;
if (answer < 0)
return -answer;
else
return 0;
}