Developer/๐ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์
[ํ๋ก๊ทธ๋๋จธ์ค/python] ํ๊ฒ ๋๋ฒ
dalpaeng
2020. 7. 14. 16:45
๋ฌธ์ ์ค๋ช
n๊ฐ์ ์์ด ์๋ ์ ์๊ฐ ์์ต๋๋ค. ์ด ์๋ฅผ ์ ์ ํ ๋ํ๊ฑฐ๋ ๋นผ์ ํ๊ฒ ๋๋ฒ๋ฅผ ๋ง๋ค๋ ค๊ณ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด [1, 1, 1, 1, 1]๋ก ์ซ์ 3์ ๋ง๋ค๋ ค๋ฉด ๋ค์ ๋ค์ฏ ๋ฐฉ๋ฒ์ ์ธ ์ ์์ต๋๋ค.
-1+1+1+1+1 = 3
+1-1+1+1+1 = 3
+1+1-1+1+1 = 3
+1+1+1-1+1 = 3
+1+1+1+1-1 = 3
์ฌ์ฉํ ์ ์๋ ์ซ์๊ฐ ๋ด๊ธด ๋ฐฐ์ด numbers, ํ๊ฒ ๋๋ฒ target์ด ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง ๋ ์ซ์๋ฅผ ์ ์ ํ ๋ํ๊ณ ๋นผ์ ํ๊ฒ ๋๋ฒ๋ฅผ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์๋ฅผ return ํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
ํ์ด
answer = 0
def solution(numbers, target):
def fo_2(x, t, li, g):
global answer
x += 1
for i in range(2):
if i == 1 : q = li[x] * -1
else : q = li[x]
t = t[0:x]
t.append(q)
if x != len(li)-1 : fo_2(x, t, li, g)
else :
if sum(t) == g: answer += 1
fo_2(-1, [], numbers, target)
return answer