본문 바로가기
Problem Solving/SWEA

[SWEA|파이썬] 5948. 새샘이의 7-3-5 게임 (D3)

by 청량리 물냉면 2023. 5. 16.
반응형
문제

https://tinyurl.com/2qq9o9nt

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

🐍파이썬
T = int(input())
for test_case in range(1, T+1):
    arr = list(map(int, input().split()))
    ans = 0
    ans_set = set()
    for i in range(7):
        for j in range(7):
            for k in range(7):
                if i != j and j != k and k != i:
                    ans_set.add(arr[i] + arr[j] + arr[k])
    print("#{} {}".format(test_case, sorted(ans_set)[-5]))

완전탐색으로 arr을 3번 돌면서 이미 뽑은 수를 제외하고 3개의 수를 뽑았다.

세 수의 총합을 set 자료구조에 넣어 중복을 제거한 뒤 정렬하고 뒤에서 5번째에 있는 수(=5번째로 큰 수)를 출력하였다.

 

 

다른 풀이 방법

from itertools import combinations
T = int(input())

for i in range(T):
    numbers = list(set(map(int, input().split(' '))))
    comb = combinations(numbers, 3)    
    comb_sum = [sum(cmb) for cmb in comb]    
    comb_sum = list(set(comb_sum))  
    comb_sort = sorted(comb_sum, reverse = True)
    
    print("#{} {}".format(i+1, comb_sort[4]))

itertools의 combinations를 이용해 3개의 수를 뽑은 뒤 더하는 방식의 풀이

(풀이참고: https://tinyurl.com/2m9x4r2z)

반응형