1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | # 1, 2, 3 더하기 class Counter(object): save = None def init_save(self, N): if N < 3: N = 3 self.save = [None] * (N+1) self.save[1] = 1 self.save[2] = 2 self.save[3] = 4 def count_of_combination(self, N): if N < 1: return 0 elif self.save[N] != None: return self.save[N] else: result = (self.count_of_combination(N-1) + self.count_of_combination(N-2) + self.count_of_combination(N-3)) self.save[N] = result # print('save[{}]={}'.format(N, result)) return result def main(): T = int(input()) counter = Counter() for i in range(T): N = int(input()) counter.init_save(N) cnt = counter.count_of_combination(N) print(cnt) if __name__ == '__main__': main() | cs |
'Algorithm' 카테고리의 다른 글
[Algorithm/Data Structure] Tree C/C++ example code (+ Mirror Tree) (0) | 2019.06.16 |
---|---|
알고리즘 문제 해결 전략 - 사탕 나눠주기 문제 (1) | 2019.03.27 |
[Algorithm] 코드 읽기 (0) | 2018.04.01 |
[Algorithm] 알고리즘 구현 접근법 (0) | 2018.03.25 |
[algorithm] Sorting(정렬)의 종류 - PQ, Merge, Quick (0) | 2017.12.16 |
WRITTEN BY
- hojongs
블로그 옮겼습니다 https://hojongs.github.io/