백준 2839 python [설탕 구하기]

인지용·2025년 2월 4일
0

알고리즘

목록 보기
34/46
post-thumbnail

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

import sys

# with open("./data.txt", "r") as file:
#     def input():
#         return file.readline().strip()

def input():
    return sys.stdin.readline().strip()

N = int(input())

if(N % 5 == 0):
    print(N // 5)
else:
    p = 0
    
    while N > 0:
        N -= 3
        p += 1

        if(N % 5 == 0):
            print(N//5 + p)
            break
        
        if(N == 1 or N == 2):
            print(-1)
            break
        
        if(N == 0):
            print(p)
            break

5로 나누어 떨어질 때까지 3을 계속 빼주면 된다.

사실 이번 문제는 정답을 보고 말았다..

dp 문제이기에 무조건 배열을 써야겠지?

dp 배열들의 값을 어떻게 조합해야되지? 등등

너무 고정관념에 사로 잡혀 더 넓은 시야로 보지 못했다.

dp 문제라고 하여 꼭 배열을 쓸 필요도 없다.

아무튼 아래 블로그를 참조하였는데 어렵네

https://puleugo.tistory.com/27

profile
한-줄

0개의 댓글