본문 바로가기
HACKERRANK/CODE

[HACKERRANK] - Recursive Digit Sum

by 나른한 사람 2021. 2. 4.

www.hackerrank.com/challenges/recursive-digit-sum/problem

 

Recursive Digit Sum | HackerRank

recursively sum all digits in a number until there is only one left

www.hackerrank.com

INPUT : n, k

 수 n을 k번 나열했을 때, 각 자리의 합을 재귀적으로 구해 한 자리 수의 답을 출력하는 문제

 

<My Solution>

k의 영향을 받는 최초의 결과는 n의 각 자리 숫자의 합의 k배

이후 res가 10보다 작아질 때 까지 반복

def superDigit(n, k):
    res = sum(list(map(int,str(n))))*k
    while res>=10: 
        res = sum(list(map(int,str(res))))
    return res

댓글