Hi, everyone. There are two line segments A and B above the vertical line. Each segment can be represented by two numbers: its center position and its length. At this time, write a program to find the length of the overlapping section between A and B according to the instructions. Each line segment is expressed as (center point, length) as follows. That's the problem
//python
a = input().split(',')
b = input().split(',')
midA = int(a[0])
maxA = int(a[1])
minA = midA - (maxA-midA)
midB = int(b[0])
maxB = int(b[1])
minB = midB - (maxB-midB)
n = []
if minA <= maxB and maxA >= minB :
n += minA, maxB, maxA, minB
n.sort()
print(n[2]-n[1])
else:
print("0")
I solved it like this, and I think the answer is coming out right. Is it possible to solve it in another way?
python
If the distance from the center of A to the center of B * 2 is less than the length of A + the length of B, the lines will likely overlap.
© 2025 OneMinuteCode. All rights reserved.