[백준/파이썬] 16019번 풀이
업데이트:
문제 정보
- 문제 출처: 백준 온라인 저지
- 문제 링크: 16019번 문제
- 문제풀이 코드 GitHub 링크
- 제출 언어: Python 3
풀이
문제
You decide to go for a very long drive on a very straight road. Along this road are five cities. As you travel, you record the distance between each pair of consecutive cities.
You would like to calculate a distance table that indicates the distance between any two of the cities you have encountered.
입력 요약
The first line contains 4 positive integers less than 1000, each representing the distances between consecutive pairs of consecutive cities: specifically, the ith integer represents the distance between city i and city i + 1.
출력 요약
The output should be 5 lines, with the ith line (1 ≤ i ≤ 5) containing the distance from city i to cities 1, 2, … 5 in order, separated by one space.
코드
import sys;read=sys.stdin.readline
l=list(map(int,read().split()))
for i in range(5):
r=[0]*5
for j in range(i+1,5):r[j]=r[j-1]+l[j-1]
for j in range(i-1,-1,-1):r[j]=r[j+1]+l[j]
print(*r)
설명
핵심은 구현 관점에서 You decide to go for a very long drive on a very straight road. Along this road are five cities. …를 만족하도록 로직을 구성하는 것입니다.
코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.
경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.
댓글남기기