블랙잭_2798번

[Python] 백준 2798번 풀이

블랙잭

1.모든 조합의 경우의 수를 따지는 문제이다.
파이썬에서는 itertools의 combinations를 이용해 조합을 따질 수 있다.

itertools.combinations에서 확인할 수 있다.

2.combinations는 combinations object를 리턴하는데 이를 for루프에 돌려 하나의 조합을 확인하게되면 튜플 을 리턴한다.
예를들어,

1
2
3
4
li = [3,4,5]
for i in itertools.combinations(li,2):
print(i)
## >> (3,4) , (3,5) , (4,5) 를 출력한다.

3.따라서 입력받은 N개의 숫자 리스트로부터 모든 조합을 따지면서 그 값이 상한값을 넘지않는 경우 새로운 변수에 입력해주면 된다.

1
2
3
4
5
6
7
com_li = 0

for i in itertools.combinations(li,3):
s = sum(i)
if s > com_li and s <= m:
com_li = s
else: pass

Full Code

Full Code

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×