[백준 2961 파이썬] 도영이가 만든 맛있는 음식

일단 해볼게·2023년 3월 29일
0

백준

목록 보기
110/132

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

import sys
from itertools import combinations

input = sys.stdin.readline

N = int(input())
arr = [] 

for _ in range(N):
    arr.append(list(map(int, input().split())))

com = []

for i in range(1, N+1):
    com.append(list(combinations(arr, i))) 

answer = 1000000000
for i in com:
    for j in i: # i는 개수에 따른 조합들, j는 조합들 중 하나
        x = 1 # 곱
        y = 0 # 합
        for z in j: # z는 조합들 중 하나의 리스트
            x *= z[0]
            y += z[1]
        answer = min(answer, abs(x - y))

print(answer)

조합을 이용해 3중 for문으로 해결했다. 복잡하다.

profile
시도하고 More Do하는 백엔드 개발자입니다.

0개의 댓글