Python/브루트 포스

[Algorithm] 백준 2309 일곱 난쟁이 | 파이썬

유일리 2024. 8. 9. 16:51

※ 2309 일곱 난쟁이

https://www.acmicpc.net/problem/2309

 

문제 해결 TIP

반대로 생각했을 때 9개의 난쟁이 키를 모두 합한 후, 2명의 난쟁이 키를 빼준 것이 100일 때를 출력하면 된다. 

 

전체 코드

result = []
for i in range(9):
    height = int(input())
    result.append(height)
result.sort()
sum = sum(result)
answer = sum
found = False

while True:
    for i in range(8):
        for j in range(i+1, 9):
            answer -= result[i]
            answer -= result[j]
            if answer == 100:
                result.remove(result[j])
                result.remove(result[i])
                found = True
                break
            else:
                answer = sum
        if found:
            break  # 'while' 루프 탈출
    if found:
        break  # 'while' 루프 탈출

for x in result:
    print(x)