Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

44-xxubin04 #163

Merged
merged 2 commits into from
May 21, 2024
Merged

44-xxubin04 #163

merged 2 commits into from
May 21, 2024

Conversation

xxubin04
Copy link
Member

@xxubin04 xxubin04 commented May 6, 2024

πŸ”— 문제 링크

6603번: λ‘œλ˜πŸ’°


βœ”οΈ μ†Œμš”λœ μ‹œκ°„

30λΆ„


✨ μˆ˜λ„ μ½”λ“œ

1. 문제 이해

μž…λ ₯λ°›λŠ” μ§‘ν•©μ˜ μ›μ†Œλ“€ 쀑 6개둜 λ§Œλ“€ 수 μžˆλŠ” λͺ¨λ“  쑰합을 좜λ ₯ν•˜λŠ” λ¬Έμ œμ΄λ‹€.
좜λ ₯ν•  λ•Œ, 각 ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ 사이에 빈 쀄이 ν•˜λ‚˜ μžˆμ–΄μ•Ό ν•œλ‹€..!


2. combinations

from itertools import combinations
input = open(0).readline

while (numComb := list(map(int, input().split()))) != [0]:
    for i in combinations(numComb[1:], 6):
        print(" " .join(str(j) for j in list(i)))
    print()

μž…λ ₯받은 λ¦¬μŠ€νŠΈκ°€ [0] 즉, 0이 μž…λ ₯된 κ²½μš°μ—λŠ” ν”„λ‘œκ·Έλž¨μ„ 끝낸닀.
[0]이 μ•„λ‹ˆλΌλ©΄, λ§Œλ“€ 수 μžˆλŠ” 쑰합을 ν•˜λ‚˜μ”© 좜λ ₯ν•΄μ£Όλ©΄ λœλ‹€...!
combinationsλ₯Ό μ‚¬μš©ν•˜λ©΄ 맀우 κ°„λ‹¨ν•˜κ²Œ ν’€ 수 μžˆλ‹€.


3. λ°±νŠΈλž˜ν‚Ή

input = open(0).readline 

def backTracing(numComb, comb):
    if len(comb) == 6:
        print(" ".join(map(str, comb)))
        return
    for i in numComb[1:]:
        if i not in comb and i > comb[-1]:
            comb.append(i)
            backTracing(numComb, comb)
            comb.pop()
            
while (numComb := list(map(int, input().split()))) != [0]:
    comb = []
    for start in range(1, numComb[0]-4):
        backTracing(numComb, [numComb[start]])
    print()

0이 μž…λ ₯λ˜μ§€ μ•Šμ€ κ²½μš°μ—λŠ” 6개 숫자의 μ‘°ν•©μ—μ„œ 제일 μ•žμ— 올 수 μžˆλŠ” μˆ˜κ°€ range(1, numComb[0]-4으둜 집합 S의 κ°œμˆ˜μ—μ„œ 5λ₯Ό λΊ€ 개수만큼 올 수 μžˆλ‹€.

λ§Œμ•½, 집합 S = {1,5,7,13,15,20, 33, 40, 45}라면 제일 λ§ˆμ§€λ§‰ μ‘°ν•©μœΌλ‘œ λ‚˜μ˜€λŠ” 것이 {13, 15, 20, 33, 40, 45}이닀. κ·ΈλŸ¬λ―€λ‘œ μ‘°ν•©μ˜ 제일 μ•žμ— 올 수 μžˆλŠ” μˆ˜λŠ” 1, 5, 7, 13으둜 4κ°œμ΄λ―€λ‘œ 집합 S의 개수 9μ—μ„œ 5λ₯Ό λΊ€ κ°œμˆ˜κ°€ 같은 것이닀.

[numComb[start]] 으둜 μ‘°ν•©μ˜ 맨 μ•ž 수λ₯Ό 지정해주지 μ•Šκ³  λ‹€μŒμ˜ μ½”λ“œ 뢀뢄을 μ‹€ν–‰ν•˜λ©΄ index 였λ₯˜κ°€ λ‚˜μ„œ μ‘°ν•©μ˜ 맨 μ•žμ— 올 수 μžˆλŠ” 경우λ₯Ό λ¨Όμ € μ •ν•΄μ£Όκ³  λ°±νŠΈλž˜ν‚Ήν•˜λŠ” 방법을 μƒκ°ν–ˆλ‹€.

    for i in numComb[1:]:
        if i not in comb and i > comb[-1]:
            comb.append(i)
            backTracing(numComb, comb)
            comb.pop()

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

λ°±νŠΈλž˜ν‚Ήμ΄ 아직 μ΅μˆ™ν•˜μ§€ μ•Šμ•„μ„œ μ—¬λŸ¬ 문제 풀어봐야 ν•  것 κ°™λ„€μš”..πŸ€”
λ°±νŠΈλž˜ν‚ΉμœΌλ‘œ ν‘Ό 방법이 좜λ ₯초과 λ¬Έμ œκ°€ ν•΄κ²°λ˜μ§€ μ•Šμ•˜λ‹€κ°€ PR μ˜¬λ¦¬λ©΄μ„œ ν•΄κ²°ν•΄μ„œ μ½”λ“œνŒŒμΌμ€ λͺ» μ˜¬λ ΈμŠ΅λ‹ˆλ‹Ή

Copy link
Collaborator

@9kyo-hwang 9kyo-hwang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 문제 μ΅μˆ™ν•˜λ‹€ μ‹Άμ—ˆλŠ”λ° μ˜†λ™λ„€ λŒλ‹€κ°€ ν’€μ—ˆλ˜ λ¬Έμ œλ„€μš”...
κ·Έλ•Œλ„ 파이썬 combinations κ°œμ‚¬κΈ°! ν–ˆλ˜ 기얡이 γ…‹γ…‹
직접 λ°±νŠΈλž˜ν‚Ή κ΅¬ν˜„ν•΄μ„œ ν’€μ–΄λ³Έ κ±° μ™„μ „ κ΅Ώ πŸ‘

from itertools import combinations
input = open(0).readline

while (S := list(map(str, input().split()))) and S[0] != 0:
    for comb in combinations(S[1:], 6):
        print(' '.join(comb))
    print()

Copy link
Member

@gjsk132 gjsk132 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λŠ¦μ–΄μ„œ λ°±νŠΈλž˜ν‚Ή 연속 2문제..! κ·Έλž˜μ„œ κ·ΈλŸ°μ§€ λ‚˜λ¦„ μŠ€ν”Όλ“œν•˜κ²Œ μ½”λ“œκ°€ μ§œμ‘ŒμŠ΅λ‹ˆλ‹€πŸ₯²

μ•½κ°„ λ‹€λ₯Έ 점은 backtracking에 λ„£λŠ” λ§€κ°œλ³€μˆ˜λ₯Ό comb λ¦¬μŠ€νŠΈμ— λ“€μ–΄μžˆλŠ” 숫자의 μˆ˜μ™€ λ§ˆμ§€λ§‰ 수(κ°€μž₯ 큰 수)둜 μ§€μ •ν–ˆμŠ΅λ‹ˆλ‹€!

μ½”λ“œ!
input = open(0).readline

def backtracking(cnt, last):
    if cnt == 6:
        print(*comb)
        return
    
    for num in lotto:
        if num < last or num in comb:
            continue
        comb.append(num)
        backtracking(cnt+1, num)
        comb.pop()

while True:
    lotto = list(map(int,input().split()))

    if lotto.pop(0) == 0:
        break

    lotto.sort()
    comb = []

    backtracking(0, 0)
    print()

@xxubin04 xxubin04 merged commit 0a4241e into AlgoLeadMe:main May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants