boj

백준 9095 1,2,3 더하기 -python

boj-9095

Posted by 동식이 블로그 on August 10, 2019

백준 9095 1,2,3 더하기 -python


너무 어렵지 않은 완전 탐색 문제집 4번문제 !!


문제풀이

  • 1,2,3으로 만들수있는 모든 수를 구해서 비교


code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import itertools

arr = [1,2,3]
N = int(input())
n = []
for _ in range(N):
    n.append(int(input()))

res = []
for x in n:
    cnt = 0
    for i in range(1,x+1):
        for j in itertools.product(arr,repeat=i):
            if(sum(j) == x):
                cnt += 1
    res.append(cnt)
print(*res,sep="\n")