Python 编程:计算学生总分,找出学习小状元和最高/最低分数
{
"title": "小明同学所在的班级共有N名同学,期末考试时进行了数学、语文、英语、 地理四门功课的测试。现要将班里总分最高的同学评为“学习小状元”。\n\n请同学们帮助计算:\n\n(1)每位同学的总分是多少?\n\n(2)找出总分最高的那位同学。\n\n(3)找出所有成绩中最高的分数和最小的分数。\n\n输入描述\n\n第一行:有一个整数N,表示有N名同学(已知1≤N≤30 )。\n\n后N行:每两行代表一个人的信息记录:包括学生的姓名及4门功课的分数:其中分数为整数,每个分数互不相同(不需判断),各数据之间空1个格。\n\n输出描述\n\n前N行:每行一个数据是每位同学的总分;\n\n第N+1行:是总分最高的那位同学的姓名;\n\n第N+2行:是所有成绩中最高的分数和最小的分数(空1个格)\n\n【要求】\n\n每一行的数据都从第一列开始输出。",
"content": "解法一:\npython\n# 输入\nN = int(input())\nstudents = []\nfor _ in range(N):\n name = input()\n scores = list(map(int, input().split()))\n students.append((name, scores))\n\n# 计算每位同学的总分\ntotal_scores = []\nfor student in students:\n total_score = sum(student[1])\n total_scores.append(total_score)\n\n# 找出总分最高的同学\nmax_score = max(total_scores)\nmax_student = students[total_scores.index(max_score)][0]\n\n# 找出所有成绩中最高的分数和最小的分数\nmax_grade = max([max(scores) for _, scores in students])\nmin_grade = min([min(scores) for _, scores in students])\n\n# 输出\nfor total_score in total_scores:\n print(total_score)\nprint(max_student)\nprint(max_grade, min_grade)\n\n\n解法二:\npython\n# 输入\nN = int(input())\nstudents = []\nfor _ in range(N):\n name = input()\n scores = list(map(int, input().split()))\n students.append((name, scores))\n\n# 计算每位同学的总分\ntotal_scores = [sum(student[1]) for student in students]\n\n# 找出总分最高的同学\nmax_score_index = total_scores.index(max(total_scores))\nmax_student = students[max_score_index][0]\n\n# 找出所有成绩中最高的分数和最小的分数\nmax_grade = max(max(scores) for _, scores in students)\nmin_grade = min(min(scores) for _, scores in students)\n\n# 输出\nfor total_score in total_scores:\n print(total_score)\nprint(max_student)\nprint(max_grade, min_grade)\n
}
原文地址: https://www.cveoy.top/t/topic/pEPE 著作权归作者所有。请勿转载和采集!