编程介的小学生 2020-04-16 01:44 采纳率: 0.4%
浏览 97

Coverage 是怎么实现的

Problem Description
A cell phone user is travelling along a line segment with end points having integer coordinates. In order for the user to have cell phone coverage, it must be within the transmission radius of some transmission tower. As the user travels along the path, cell phone coverage may be gained (or lost) as the user moves inside the radius of some tower (or outside of the radii of all towers). Given the location of up to 100 towers and their transmission radii, you are to compute the percentage of cell phone coverage the user has
along the specified path. The (x,y) coordinates are integers between -100 and 100, inclusive, and the tower radii are integers between 1 and 100, inclusive.

Input
Your program will be given a sequence of configurations, one per line, of the form: N C0X C0Y C1X C1Y T1X T1Y T1R T2X T2Y T2R ... Here, N is the number of towers, (C0X,C0Y) is the start of path of the cell phone user,
(C1X,C1Y) is the end of the path, (TkX,TkY) is the position of the kth tower, and TkR is its transmission radius. The start and end points of the paths are distinct. The last problem is terminated by the line 0

Output
For each configuration, output one line containing the percentage of coverage the cell phone has, rounded to two decimal places.

Sample Input
3 0 0 100 0 0 0 10 5 0 10 15 0 10
1 0 0 100 0 40 10 50
0

Sample Output
25.00
88.99

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间 优质创作者: Java、后端开发技术领域 2024-07-25 15:19
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    ```python import math def calculate_coverage(towers, start, end):
    total_distance = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
    covered_distance = 0
    
    for tower in towers:
        distance_to_tower = math.sqrt((tower[0] - start[0]) ** 2 + (tower[1] - start[1]) ** 2)
        if distance_to_tower <= tower[2]:
            covered_distance += min(total_distance, 2 * math.sqrt(tower[2] ** 2 - distance_to_tower ** 2))
    
    return (covered_distance / total_distance) * 100
    

    while True: towers_input = list(map(int, input().split())) if towers_input[0] == 0: break

    num_towers = towers_input[0]
    start = (towers_input[1], towers_input[2])
    end = (towers_input[3], towers_input[4])
    towers = [(towers_input[i], towers_input[i+1], towers_input[i+2]) for i in range(5, 5 + num_towers * 3, 3)]
    
    coverage = calculate_coverage(towers, start, end)
    print("{:.2f}".format(coverage))
    

    Sample Input

    3 0 0 100 0 0 0 10 5 0 10 15 0 10

    1 0 0 100 0 40 10 50

    0

    Sample Output

    25.00

    88.99

    评论

报告相同问题?