티스토리 뷰

728x90
반응형

 

https://www.acmicpc.net

 

Baekjoon Online Judge

Baekjoon Online Judge 프로그래밍 문제를 풀고 온라인으로 채점받을 수 있는 곳입니다.

www.acmicpc.net

 

 


 

 

<나의 풀이>

from itertools import combinations

n, s = map(int, input().split())
nums = list(map(int, input().split()))

result = 0

for i in range(1, n+1):
    combin = list(combinations(nums, i))
    for j in combin:
        if sum(j) == s:
            result += 1

print(result)

 

 

<다른 사람의 풀이>

# lys74423님 코드

import sys
input=sys.stdin.readline
from collections import deque

def dfs(x,Sum):
    global cnt
    if(x>n-1):
        return
    Sum+=arr[x]
    if(Sum==s):
        cnt+=1
    dfs(x+1,Sum)
    dfs(x+1,Sum-arr[x])

n,s=map(int,input().split())
arr=list(map(int,input().split()))
cnt=0
dfs(0,0)
print(cnt)
    
    
# <배운점> 
# => dfs를 이용한 점

 

# park345601님 코드

import sys
from itertools import combinations

IN = sys.stdin.readline().split()
while IN[0] != '0':
    cm = combinations(IN[1:], 6)
    for c in cm:
        print(' '.join(c))
    print()
    IN = sys.stdin.readline().split()
    

# <배운점> 
# => 코드를 간략하게 짠 점


# 더 간략히
# pse1202님 코드
# n,s=map(int,input().split());x=[0]
# for i in map(int,input().split()):x+=[i+b for b in x]
# print(x.count(s)-(s==0))

 

728x90
반응형
댓글