[HackerRank][Python3] Recursive Digit Sum
2018. 6. 7. 02:44 |
프로그래밍/HackerRank
Problem :
https://www.hackerrank.com/challenges/recursive-digit-sum/problem
My Solution :
#!/usr/bin/env python3 memo = {i:i for i in range(10)} def super_digit(n): if n in memo: return memo[n] tmp_n = sum(map(int, str(n))) if tmp_n in memo: return memo[tmp_n] memo[tmp_n] = super_digit(tmp_n) return memo[tmp_n] def digitSum(n, k): tmp_n = super_digit(n) return super_digit(tmp_n*k) n, k = map(int, input().strip().split()) result = digitSum(n, k) print(result)
My Solution 2 :
생각해보니 무조건 작아지고 분기하지 않기 때문에 memoization이 사용될 일이 없다. 그래서 다시 아래와 같이 풀어봄.
#!/usr/bin/env python3 def super_digit(n): if n < 10: return n tmp_n = sum(map(int, str(n))) return super_digit(tmp_n) def digitSum(n, k): return super_digit(n*k) n, k = map(int, input().strip().split()) result = digitSum(n, k) print(result)
Super Simple Solution :
n, k = map(int, input().strip().split()) print(n*k%9 or 9)
'프로그래밍 > HackerRank' 카테고리의 다른 글
[HackerRank][Python3] Is This a Binary Search Tree? (2) | 2018.06.25 |
---|---|
[HackerRank][Python3] Candies (0) | 2018.06.14 |
[HackerRank][Python3] Two Characters (0) | 2018.06.11 |
[HackerRank][Python3] Append and Delete (0) | 2018.06.10 |
[HackerRank][Python3] Sam and substrings (0) | 2018.06.06 |
[HackerRank][Python3] Marc's Cakewalk (0) | 2018.06.06 |
[HackerRank][Python3] Minimum Loss (0) | 2018.06.05 |
[HackerRank][Python3] Strong Password (0) | 2018.06.05 |
최근에 달린 댓글 최근에 달린 댓글