【精选】Python入门算法题(三)

版权声明:原创文章,转载请注明 https://blog.csdn.net/qq_37482202/article/details/82388482

今天无意中发现一道算法题很有意思,题不难,都是初中的知识,主要就是求一个固定点到一条直线的最短距离,我用了一个非常麻烦的方法才求出,我先是用余弦定理求出夹角再判断用勾股定理结合方程组解出来,很是麻烦,还画了几张图,我就问我以为朋友,他说很简单啊,拿线上两点就求出来了线的斜率然后结合点到直线距离公式就出来了。

可能我描述的不是很清楚,先来看一下题:

Brio got his house constructed near the National Highway. The Construction and Planning Committee has planned to construct a road near his house. Brio is worried about the Committee's plan as he fears that his house might come on the way of the road being constructed. In such a case, he needs to request the Committee members to replan the construction. Assume that Brio's house is a circle with center at (0,0) and radius r. Given 2 integral points (x1,y1) and (x2,y2) through which the road passes you need to give output as mentioned below:
if the road crosses his house, print "REPLANNING" (quotes for clarity). if the road just touches his house, print "JUST MISSED" if the road is far off his house, print "SAFE"
INPUT
The first line of the input contains the number of testcases T. Each of the next T test cases has 2 lines, the first containing 4 space separated integers x1 y1 x2 y2 and the second line contains r.

OUTPUT
One line per test case containing the required output.

CONSTRAINTS
T<=10000
-10000 <= x1 y1 x2 y2 <= 10000
r <= 10000

示例输入:
2
5 5 -5 5
4
1 2 3 4
5
示例输出:
SAFE
REPLANNING

这道题的意思就是说有个人叫Brio,他有个房子,政府想在他家旁边修条路,但是他怕吵,想看看什么距离不会吵,假设Brio的房子是一个圆心,中心位于(0,0)和半径r。给定道路经过的2个积分点(x1,y1)和(x2,y2),你需要输出如下所述的输出:

如果道路穿过他的房子范围,请打印“REPLANNING”(为清晰起见,引号)。如果这条路刚刚触及他的房子范围,如果道路远离他的房子范围,请打印“JUST MISSED”,打印“SAFE”

两个点和安全距离都是用户输入,用户先输入一个数代表要输入几组数据,然后每输入4个数代表两点坐标,再输入一个安全距离,这么循环输入。

大概就是这么个意思,有英语好的自己翻译一下。

主要就是求点到直线的距离吗,我们一边看代码一边说

import math
import numpy
def get_distance(x1, y1, x2, y2):
    #如果x1=x2,说明是一条竖直的线,没有斜率,距离就是x的绝对值
    if (x1 - x2) == 0:
        return math.fabs(x1)
    #如果y1=y2,说明是一条水平的线,斜率为0,距离就是y的绝对值
    if (y1 - y2) == 0:
        return math.fabs(y1)
    #初中的知识,给定两点求斜率
    k=math.fabs(y2-y1)/math.fabs(x2-x1)
    #y=kx+c,求出c
    c=y1-k*x1
    #先换成一般式,再用点到直线距离公式求出距离
    distance=math.fabs(c)/math.sqrt(k**2+1)
    return distance

def compare(distance,r):
    if distance>r:
        print("SAFE")
    elif distance==r:
        print("JUST MISSED")
    else:
        print("REPLANNING")

def del_input(numbers):
    num=len(numbers)
    for x in range(num):
        #得到房子和路的距离
        distance=get_distance(numbers[x][0],numbers[x][1],numbers[x][2],numbers[x][3])
        #比较一下安全不安全,输出指定的字符串
        compare(distance,numbers[x][4])

if __name__ == '__main__':
    #从键盘得到的输入会变成字符串,需要转换一下
    num=int(input("请输入有几组数据:"))
    #使用numpy生成num行,5列的二维数组,用来存储数据
    numbers=numpy.zeros(shape=(num,5))
    #循环获取每一行的数据,前四个存储坐标,第五个存储安全距离
    for x in range (num):
        list=input("请以空格隔开输入四个数字:").split(" ")
        for y in range(4):
            numbers[x][y]=int(list[y])
        numbers[x][4]=input("请输入安全距离:")
    #把得到的数据传入函数进行分批运算
    del_input(numbers)

猜你喜欢

转载自blog.csdn.net/qq_37482202/article/details/82388482