하하 ..

자신감있게 문제 풀고 제출 했는데 계속 틀려서 영문을 모르다가..

다른 분들 코드를 살짝 보고 예외값이 있다는 것을 깨달았다!!

ㅇㅖ로, L, P, V 가 3, 8, 20 일 경우

V % P 값(4) 이 L(3)값보다 커서 원래 답인 9가 아닌 10이 나오게 된다.

따라서 그 부분을 if문으로 넣어 수정해주었다. 

 

먼저 처음 제출했던 오답 코드

i = 1
while True:
    L, P, V = list(map(int, input().split()))
    if L == 0 and P == 0 and V == 0:
        break
    answer = int(L * (V // P) + (V % P))

    print('Case ' + str(i) + ': ' + str(answer))
    i += 1

 

수정코드1:

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

    # 나머지가 V값 보다 크면 나머지에 V값을 넣어서 계산한다.
    rest = V % P
    if rest > L:
        rest = L
    answer = int(L * (V // P) + (rest))
    print('Case ' + str(i) + ': ' + str(answer))

수정코드2:

#다른 식
i = 0
while True:
    i += 1
    L, P, V = list(map(int, input().split()))
    if L == 0 and P == 0 and V == 0:
        break

    answer = int(L * (V // P) + min((V % P), L))
    print('Case ' + str(i) + ': ' + str(answer))

 

반응형

+ Recent posts