|
发表于 2021-9-10 00:07:27
|
显示全部楼层
本楼为最佳答案
 - def overlap(A: list[tuple, int, int], B: list[tuple, int, int]):
- width = 0
- height = 0
- a, b = (A[0][0]-A[1]/2, A[0][0]+A[1]/2), (B[0][0]-B[1]/2, B[0][0]+B[1]/2)
- if a[0] < b[0] and a[1] > b[0]:
- if a[1] > b[1]:
- width = b[1]-b[0]
- elif a[1] < b[1]:
- width = a[1]-b[0]
- elif b[0] < a[0] and b[1] > a[0]:
- if b[1] > a[1]:
- width = a[1]-a[0]
- elif b[1] < a[1]:
- width = b[1]-a[0]
- a, b = (A[0][1]-A[2]/2, A[0][1]+A[2]/2), (B[0][1]-B[2]/2, B[0][1]+B[2]/2)
- if a[0] < b[0] and a[1] > b[0]:
- if a[1] > b[1]:
- height = b[1]-b[0]
- elif a[1] < b[1]:
- height = a[1]-b[0]
- elif b[0] < a[0] and b[1] > a[0]:
- if b[1] > a[1]:
- height = a[1]-a[0]
- elif b[1] < a[1]:
- height = b[1]-a[0]
- return width*height
- def main():
- N = int(input("輸入投擲水袋的數量:"))
- area = []
- for n in range(N):
- x, y, w, h = map(int, input("水袋位置 x y,噴灑寬度 w,和高度 h(以空格隔開輸入):").split())
- area.append([(x, y), w, h])
- total = 0
- res = 0
- for i in range(N):
- total += area[i][1]*area[i][2]
- for j in range(i+1, N):
- res += overlap(area[i], area[j])
- print(f"灑水範圍:{total-res}")
- if __name__ == "__main__":
- main()
复制代码- 輸入投擲水袋的數量:3
- 水袋位置 x y,噴灑寬度 w,和高度 h(以空格隔開輸入):35 65 30 30
- 水袋位置 x y,噴灑寬度 w,和高度 h(以空格隔開輸入):75 35 30 20
- 水袋位置 x y,噴灑寬度 w,和高度 h(以空格隔開輸入):100 55 40 40
- 灑水範圍:3000.0
复制代码 |
|