Archive/Questions

[programmers/Swift] 부족한 금액 계산하기

Marco 2022. 5. 10. 20:08
728x90

programmers.co.kr - 코딩테스트연습 - Lv.1 - 위클리 챌린지 - 부족한 금액 계산하기

Constraints :

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

 

Solution.swift :

//
//  Created by Yongwoo Marco on 2022/05/05.
//  Copyright © 2022 Yongwoo Marco Kim. All rights reserved.
//

func solution(_ price:Int, _ money:Int, _ count:Int) -> Int64{
    let sum = Int64((1...count).reduce(0) { $0 + $1 * price })
    let answer = sum - Int64(money)
    return answer < 0 ? 0 : answer
}

print(solution(3, 20, 4)) // 10

How I tried this :
횟 수에 가격을 곱한 값을 모두 더해서 총 금액을 구함.

문제에 관한 모든 저작권 : https://programmers.co.kr/

728x90