알고리즘
-
[프로그래머스][Swift] 자릿수 더하기알고리즘 2022. 6. 26. 22:47
func solution(_ n:Int) -> Int { var answer: Int = n var result: [Int] = [] var value: Int = Int() while answer >= 10 { value = answer % 10 answer = answer / 10 result.append(value) } result.append(answer) return result.reduce(0) { $0 + $1 } } func solution2(_ n:Int) -> Int { var result: Int = Int() for (_, num) in String(n).enumerated() { result += Int(String(num)) ?? 0 } return result } print(s..