[PS] 그리디: 4796 캠핑

devhans·2024년 8월 26일
0

PS

목록 보기
5/20
post-thumbnail

문제 출처

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

코드

import sys
fast_input = sys.stdin.readline
fast_print = sys.stdout.write

test_cases = {}
idx = 1
while True:
    temp_line = sys.stdin.readline().rstrip()
    if temp_line == "0 0 0":
        break
    test_cases[idx] = temp_line.split(" ")
    idx += 1

for case_idx, test_case in test_cases.items():
    limit_days = int(test_case[0])
    consecutive_days = int(test_case[1])
    vacation_days = int(test_case[2])
    use_days = 0

    use_days += (vacation_days // consecutive_days) * limit_days
    vacation_days = vacation_days % consecutive_days

    if vacation_days >= limit_days:
        use_days += limit_days
    else:
        use_days += vacation_days

    fast_print("Case " + str(case_idx) + ": " + str(use_days))

코멘트

좀 더 스마트하게 바꿀 수 있다.
실제로 fast_input도 정의해놓고 사용하지 않았고..
쓸데없이 딕셔너리를 사용한것 같다.
하나의 while문 안에서 처리가 가능하다.

import sys
fast_input = sys.stdin.readline
fast_print = sys.stdout.write

i = 1
while True:
    L, P, V = map(int, fast_input().split())
    if L == 0 and P == 0 and V == 0:
        break

    else:
        if V % P > L:
            fast_print("Case " + str(i) + ": " + str(V // P * L + L))
        else:
            fast_print("Case " + str(i) + ": " + str(V // P * L + V % P))
        i += 1
profile
책 읽고 운동하기

0개의 댓글