[백준/파이썬] 14541번 풀이

업데이트:



문제 정보


풀이

문제

Basri, Lim and Rama are taking a road trip during their semester break. But the odometer in their car is broken, so they don’t know how many miles they have driven. Fortunately, Bill has a working stopwatch, so they can record their speed and the total time they have driven. Unfortunately, their record keeping strategy is a little odd, so they need help computing the total distance driven. …

입력 요약
The input consists of one or more data sets. Each set starts with a line containing an integer n, (1 ≤ n ≤ 10), followed by n pairs of values, one pair per line. The first value in a pair, s, is the speed in miles per hour and the second value, t, is the total elapsed time. …

출력 요약
For each input data set, print the distance driven, followed by a space, followed by the word “miles”.

코드

while True:
    n=int(input())
    if n==-1:break
    l=[list(map(int,input().split()))for _ in range(n)]
    h=l[0][1]
    d=l[0][0]*h
    for i in range(1,n):d,h=d+l[i][0]*(l[i][1]-h),l[i][1]
    print(d,'miles')

설명

핵심은 구현 관점에서 Basri, Lim and Rama are taking a road trip during their semester break. …를 만족하도록 로직을 구성하는 것입니다.

코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.

경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.



다음 읽을거리

관련 허브 페이지에서 같은 주제의 글을 이어서 확인할 수 있습니다.

댓글남기기