백준(Python) 2609, 9461, 1149, 1932

오성인·2023년 3월 16일
0

알고리즘

목록 보기
10/18
post-custom-banner

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

def gcd(a, b):
    if a == 0:
        return b
    elif b == 0:
        return a
    return gcd(max(a, b) % min(a, b), min(a, b))
a, b = map(int, input().split())
lcm = a * b // gcd(a,b)
print(gcd(a, b))
print(lcm)

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

dp = [0 for _ in range(101)]
dp[0], dp[1], dp[2], dp[3], dp[4] = 1, 1, 1, 2, 2
for i in range(5, 101):
    dp[i] = dp[i-1] + dp[i-5]
t = int(input())
for _ in range(t):
    n = int(input())
    print(dp[n-1])
    

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

n = int(input())
RGB_lst = list()
for _ in range(n):
    RGB_lst.append(list(map(int, input().split())))
dp = [[0, 0, 0] for _ in range(n)]
dp[0][0], dp[0][1], dp[0][2] = RGB_lst[0][0], RGB_lst[0][1], RGB_lst[0][2]


for i in range(1, n):
    dp[i][0] = min(RGB_lst[i][0] + dp[i-1][1], RGB_lst[i][0] + dp[i-1][2])
    dp[i][1] = min(RGB_lst[i][1] + dp[i-1][0], RGB_lst[i][1] + dp[i-1][2])
    dp[i][2] = min(RGB_lst[i][2] + dp[i-1][1], RGB_lst[i][2] + dp[i-1][0])

print(min(dp[n-1]))

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

n = int(input())
dp = [[0] * i for i in range(1, n+1)]
piramid = []
for i in range(1, n+1):
    piramid.append(list(map(int, input().split())))


dp[0][0] = piramid[0][0]

for i in range(1, n):
    for j in range(len(piramid[i])):
 
        if j == 0:
            dp[i][j] = dp[i-1][j] + piramid[i][j]
        elif j == i:
            dp[i][j] = dp[i-1][-1] + piramid[i][j]
        else:
            dp[i][j] = max(dp[i-1][j-1] + piramid[i][j], dp[i-1][j] + piramid[i][j])
            
print(max(dp[n-1]))
profile
기여하는 개발자
post-custom-banner

0개의 댓글