728x90
반응형

문제

 

풀이 방법

  1. 5로 나눈 몫을 count에 더하기
  2. 남은 hp를 계산
  3. 3으로 나눈 몫을 count에 더하기
  4. 남은 hp를 계산
  5. 1로 나눈 몫을 count에 더하기
  6. 이 과정에서 남은 hp가 0이거나 0미만일 경우 조건절을 빠져나와 return

 

코드

import Foundation

func solution(_ hp:Int) -> Int {
    
    var total = hp
    var count = 0
    
    if total != 0 && total > 0 {
        count += total / 5
        total -= 5 * (total / 5)
        
        if total != 0 && total > 0 {
            count += total / 3
            total -= 3 * (total / 3)
            
            if total != 0 && total > 0 {
                count += total / 1
                total -= 1 * (total / 1)
            }
        }
    }
    return count
}
728x90
반응형
욱승