求用Python写一下

img

import math
p = [0.85, 0.10, 0.05]

def funl(p):
    s = 0
    for px in p:
        s += px*math.log(px,2)
    return -s

试题一:

#-*- coding:utf-8 -*-

import math

def get_distance(x1,x2):
    ##计算两个坐标的距离
    d_x = x1[0] - x2[0]   #横坐标差
    d_y = x1[1] - x2[1]   #纵坐标差
    return round(math.sqrt(  (d_x)**2 + (d_y)**2 ),2)

def is_triangle(a,b,c):
    #判断是否是三角形,根据两边之和大于第三边,两边之差小于第三边
    t1 = a-b
    t2 = a+b
    if t1 < c < t2:
        return True
    else:
        return False

def get_area(a,b,c):
    ##求三角形面积
    p = round((a + b + c) / 2, 2)
    s = round(math.sqrt(p*(p-a)*(p-b)*(p-c)),2)
    print(s)
    return s


if __name__ == '__main__':
    ##三点坐标
    x1 = (-1, 8)
    x2 = (2, 10)
    x3 = (5, 20)

    #距离
    a = get_distance(x1,x2)
    b = get_distance(x1,x3)
    c = get_distance(x2,x3)

    flag = is_triangle(a,b,c)
    if flag:
        get_area(a,b,c)
    else:
        print('无法构成三角形')