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

업데이트:



문제 정보


풀이

문제

Several seasons of hot summers and cold winters have taken their toll on Farmer John’s fence, and he decides it is time to repaint it, along with the help of his favorite cow, Bessie. Unfortunately, while Bessie is actually remarkably proficient at painting, she is not as good at understanding Farmer John’s instructions.

If we regard the fence as a one-dimensional number line, Farmer John paints the interval between (x=a) and (x=b). For example, if (a=3) and (b=5), then Farmer John paints an interval of length 2. …

입력 요약
The first line of the input contains the integers (a) and (b), separated by a space ((a < b)).

The second line contains integers (c) and (d), separated by a space ((c < d)). …

출력 요약
Please output a single line containing the total length of the fence covered with paint.

코드

a,b=map(int,input().split())
c,d=map(int,input().split())
l=[a,b,c,d]
if c<=a<=d or a<=c<=b:
    print(max(l)-min(l))
else:
    print(b-a+d-c)

설명

핵심은 구현 관점에서 Several seasons of hot summers and cold winters have taken their toll on Farmer John’s fence, and he decides it is time to repaint it, along with the …를 만족하도록 로직을 구성하는 것입니다.

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

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



다음 읽을거리

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

댓글남기기