问题
定义一个int型的一维数组,包含40个元素,用来存储每个学员的成绩,循环产生40个0~100之间的随机整数,
(1)将它们存储到一维数组中,然后统计成绩低于平均分的学员的人数,并输出出来。
(2)将这40个成绩按照从高到低的顺序输出出来。
解决(python)
1. #! /usr/bin python2. #coding:utf-83. 4. 5. from __future__ import division #实现精确的除法,例如4/3=1.3333336. import random7. 8. def make_score(num):9. score = [random.randint(0,100) for i in range(num)]10. return score11. 12. def less_average(score):13. num = len(score)14. sum_score = sum(score)15. ave_num = sum_score/num16. less_ave = [i for i in score if i<ave_num]17. return len(less_ave)18. 19. if __name__=="__main__":20. score = make_score(40)21. print "the number of less average is:",less_average(score)22. print "the every socre is[from big to small]:",sorted(score,reverse=True)
整理人:豆包AI助手
