問題
定義一個int型的一維數組,包含40個元素,用來存儲每個學員的成績,循環產生40個0~100之間的隨機整數,
(1)將它們存儲到一維數組中,然后統計成績低于平均分的學員的人數,并輸出出來。
(2)將這40個成績按照從高到低的順序輸出出來。
解決(python)
#! /usr/bin python#coding:utf-8from __future__ import division #實現精確的除法,例如4/3=1.333333import randomdef make_score(num): score = [random.randint(0,100) for i in range(num)] return scoredef less_average(score): num = len(score) sum_score = sum(score) ave_num = sum_score/num less_ave = [i for i in score if i<ave_num] return len(less_ave)if __name__=="__main__": score = make_score(40) print "the number of less average is:",less_average(score) print "the every socre is[from big to small]:",sorted(score,reverse=True)
新聞熱點
疑難解答