토니의 연습장

완전 탐색 예제 본문

Algorithm/Ch 2. 기초 알고리즘

완전 탐색 예제

bellmake 2024. 9. 6. 14:05

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

from itertools import combinations

heights = []
for _ in range(9):
    height = int(input())
    heights.append(height)
    
for a in combinations(heights, 7):
    if sum(a) == 100:
        a = list(a) # tuple은 sort 불가하여 list로 먼저 변환
        a.sort()
        for x in a:
            print(x)
        break

 

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

 

from itertools import permutations

N = int(input())
A = list(map(int, input().split()))
answer = 0

for a in permutations(A, N):
    sum = 0
    for i in range(len(a)-1):
        sum += abs(a[i]-a[i+1])
    answer= max(answer, sum)
    
print(answer)

'Algorithm > Ch 2. 기초 알고리즘' 카테고리의 다른 글

그리디 알고리즘 예제  (0) 2024.09.10
투 포인터 예제  (1) 2024.09.06
정렬 예제  (4) 2024.09.06
이분 탐색 예제  (3) 2024.09.05