• Python【每日一问】35


    问:

    基础题:

    从键盘输入4个数字,各数字采用空格分隔,对应为变量x0,y0,x1,y1。计算(x0,y0)和(x1,y1)两点之间的距离,输出结果保留1位小数。
    比如,键盘输入:0 1 3 5,屏幕输出:5.0
    

    提高题:

    键盘输入小明学习的课程以及考试分数信息,信息之间采用空格分隔,每个课程一行,空格回车结束录入,示例格式如下:
    数学 90
    语文 95
    英语 86
    物理 84
    生物 87
    输出得分最高和最低的课程名称、考试分数,以及所有课程的平均分(保留2位小数)
    格式如下:
    最高分课程是语文 95,最低分课程是物理 84,平均分是88.4

    答:

    基础题:

    从键盘输入4个数字,各数字采用空格分隔,对应为变量x0,y0,x1,y1。计算(x0,y0)和(x1,y1)两点之间的距离,输出结果保留1位小数。
    比如,键盘输入:0 1 3 5,屏幕输出:5.0

    方法1:

    i = input('输入坐标').split()
    x1, x2, x3, x4 = eval(i[0]), eval(i[1]), eval(i[2]), eval(i[3])
    dis = pow((pow(x3 - x1, 2) + pow(x4 - x2, 2)), 0.5)
    print(dis)

    方法2:

    variable = list(input("输入四个数字:(空格分隔)").split(' '))
    x0,y0,x1,y1 = variable[0],variable[1],variable[2],variable[3]
    distance = ((eval(x0)-eval(x1))**2 + (eval(y0)-eval(y1))**2)**0.5
    print("{:.1f}".format(distance))

    方法3:

    from math import sqrt, pow
    
    
    def cal_distance(x1, y1, x2, y2):
        return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2))
    
    
    if __name__ == '__main__':
        x1, y1, x2, y2 = map(float, input("请输入坐标数字:").split())
        print(cal_distance(x1, y1, x2, y2))

    方法4:

    a = input('input your number:').split()
    x0, y0, x1, y1 = int(a[0]), int(a[1]), int(a[2]), int(a[3])
    target = float(((y0-y1)**2+(x0-x1)**2)**0.5)
    print(target)

    方法5:

    import math
    
    s1 = input("请输入2个点坐标,用逗号分隔")
    lst = s1.split(',')
    
    x0 = int(lst[0])
    y0 = int(lst[1])
    x1 = int(lst[2])
    y1 = int(lst[3])
    
    a1 = int(pow((x0 - x1), 2))
    a2 = int(pow((y0 - y1), 2))
    leng = math.sqrt(a1 + a2)
    
    
    print("({0},{1}),({2},{3})之间的距离为{4}" .format(x0, y0, x1, y1, leng))
    
    

    提高题:

    键盘输入小明学习的课程以及考试分数信息,信息之间采用空格分隔,每个课程一行,空格回车结束录入,示例格式如下:
    数学 90
    语文 95
    英语 86
    物理 84
    生物 87
    输出得分最高和最低的课程名称、考试分数,以及所有课程的平均分(保留2位小数)
    格式如下:
    最高分课程是语文 95,最低分课程是物理 84,平均分是88.4


    方法1:

    j = input('请输入课程和成绩').split()
    k = {}
    sum = 0
    for i in range(0, len(j), 2):
        k[j[i]] = eval(j[i+1])
        sum += eval(j[i+1])
    k1 = sorted(k.items(),key=lambda k : k[1])
    print('最高课和成绩:', k1[-1])
    print('最低课和成绩:', k1[0])
    print('均值:{:.2f}'.format(sum/(len(j)/2)))

    方法2:

    info_list = []
    scores, subjects = 0, 0
    while True:
        info = input("请输入小明成绩:(以空格分隔;回车结束录入)")
        if info == '':
            break
        else:
            info_list.append(info.split(' '))
            scores += eval(info.split(' ')[1])
            subjects += 1
    info_list.sort(key=lambda x: x[1], reverse=True)
    print("最高分课程是{}:{},最低分课程是{}:{},平均分是{:.1f}".format(info_list[0][0], info_list[0][1], info_list[-1][0], info_list[-1][1], scores / subjects))

    方法3:

    class Student:
        name = '姓名'
        course = 'none'
        course_score = -1
    
        def theHighestScore(self, course_score_list):
            return max(course_score_list)
    
        def theLowestScore(self, course_score_list):
            return min(course_score_list)
    
        def theAverageScore(self, course_score_list):
            sum = 0
            for score in course_score_list:
                sum += score
    
            average_score = sum / len(course_score_list)
            return average_score
    
    
    if __name__ == '__main__':
        student = Student()
        student.name = '小明'
    
        course_score_dict = {}
    
        student.course = list(map(str, input("请先输入课程名:").strip().split()))
        student.course_score = list(map(float, input("然后请输入课程对应考试分数:").strip().split()))
    
        course_score_dict = dict(zip(student.course, student.course_score))
    
        print(course_score_dict)
        theHighestScore = student.theHighestScore(student.course_score)
        theLowestScore = student.theLowestScore(student.course_score)
    
        print('最高分课程是%s %d' % (max(course_score_dict, key=course_score_dict.get), theHighestScore))
        print('最低分课程是%s %d' % (min(course_score_dict, key=course_score_dict.get), theLowestScore))
        print('平均分是', student.theAverageScore(student.course_score))

    方法4:

    c_s_list = {}  # class & score
    sum = 0            # 均值
    while True:
        a = input('input your class && score:')
    
        if a == 'esc':
            for key, value in c_s_list.items():
                print(key,value)
                sum += int(value)  # 均值
    
            max_min = sorted(c_s_list.items(), key=lambda s: s[1])
    
            print('
    得分最高的课程名称:{}考试分数:{}'.format(max_min[-1][0], max_min[-1][1]))
            print('得分最低的课程名称:{}考试分数:{}'.format(max_min[0][0], max_min[0][1]))
            print('均值:%.1f' % (sum/len(c_s_list)))
            break
        else:
            b = a.split()
            c_s_list[b[0]] = b[1]


  • 相关阅读:
    Git 分支开发规范
    小程序技术调研
    弹性布局
    vue 自定义指令的魅力
    记一次基于 mpvue 的小程序开发及上线实战
    mpvue学习笔记-之微信小程序数据请求封装
    微信小程序开发框架 Wepy 的使用
    Hbuilder 开发微信小程序的代码高亮
    luogu3807 【模板】 卢卡斯定理
    luogu1955 [NOI2015] 程序自动分析
  • 原文地址:https://www.cnblogs.com/ElegantSmile/p/10989017.html
Copyright © 2020-2023  润新知