728x90
๋ฐ˜์‘ํ˜•
# ์žฌ๊ท€ํ•จ์ˆ˜๋กœ ๋ถ€๋ถ„์ง‘ํ•ฉ ๋งŒ๋“ค๊ธฐ
# bits: ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ์š”์†Œ๋ฅผ ์‚ฌ์šฉํ• ์ง€์˜ ์œ ๋ฌด (1์€ ์‚ฌ์šฉํ•˜๊ณ  0์€ ์•„๋‹˜)
# depth : ๋‚ด๊ฐ€ ์–ผ๋งˆ๋‚˜ ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ˜ธ์ถœํ–ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์นด์šดํŠธ ๊ฐ’...
def recur(bits, depth):
    # ๊ธฐ์ € ์กฐ๊ฑด
    if depth == N:
        for i in range(N):
            if bits[i] == 1:
                print(arr[i], end=' ')
        print()
        return

    for i in range(2): # 0, 1
        bits[depth] = i
        # ์ž๊ธฐ ์ž์‹ ์„ ํ˜ธ์ถœ
        recur(bits, depth + 1)


N = 4
arr = [1, 2, 3, 4]
recur([0] * N, 0)

# result
4 
3 
3 4 
2 
2 4 
2 3 
2 3 4 
1 
1 4 
1 3 
1 3 4 
1 2 
1 2 4 
1 2 3 
1 2 3 4

๋’ค์—์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ๋Œ์•„์˜จ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์ฃผ๋ฉด ์ดํ•ดํ•˜๊ธฐ ์‰ฌ์šธ๊ฒƒ ๊ฐ™๋‹ค~~~

 

์žฌ๊ท€ํ•จ์ˆ˜ ๋„ˆ๋ฌด ์–ด๋ ค์›Œ์š”์˜ค....์ œ์ œ์ œ.....๊ธฐ.........๋ž„,,,,

 

์—ฌ๊ธฐ์„œ ์ œ์ผ ์ค‘์š”ํ•œ ํฌ์ธํŠธ๋Š” [0, 0, 0, 0] ๋ฐฐ์—ด์„ ๋๊นŒ์ง€ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ์—ญ์œผ๋กœ ๋งŒ๋“ค์–ด์ ธ ๋‚˜๊ฐ€๋Š” ๋Š๋‚Œ์ด๋ผ๋Š”๊ฑฐ?

return์„ ๋ฐ›์•˜์„ ๋•Œ, ์ œ์ผ ์ฒ˜์Œ์ด ์•„๋‹Œ ์ž์‹ ์„ ํ˜ธ์ถœํ–ˆ๋˜ ๊ณณ์œผ๋กœ ๋Œ์•„๊ฐ€ 0์ด ์•„๋‹ˆ๋ผ 1์ธ ๊ฒฝ์šฐ๋ฅผ ๋งŒ๋“ค์–ด์ค€๋‹ค๋Š”๊ฑฐ??

๊ทธ๋ž˜์„œ ๋‹ต์„ ์ถœ๋ ฅํ–ˆ์„๋•Œ, ๋ฆฌ์ŠคํŠธ์— ๊ฐ€์žฅ ๋จผ์ € ์ €์žฅ๋œ 1์ด ์•„๋‹ˆ๋ผ 4๊ฐ€ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„์ด ์ •๋ง ์ค‘์š” ํฌ์ธํŠธ์ธ๊ฑฐ ๊ฐ™๋‹ค...

 

์žฌ๊ท€์˜ ๊ฐ€์žฅ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ : ํŒฉํ† ๋ฆฌ์–ผ

 

728x90
๋ฐ˜์‘ํ˜•

+ Recent posts