问题

定义一个20*5的二维数组,用来存储某班级20位学员的5门课的成绩;这5门课按存储顺序依次为:core C++,coreJava,Servlet,JSP和EJB。

(1)循环给二维数组的每一个元素赋0~100之间的随机整数。

(2)按照列表的方式输出这些学员的每门课程的成绩。

(3)要求编写程序求每个学员的总分,将其保留在另外一个一维数组中。

(4)要求编写程序求所有学员的某门课程的平均分。

解决(python)

  1. #! /usr/bin/env python
  2. #coding:utf-8
  3. from __future__ import division
  4. import random
  5. def score(score_list,course_list,student_num):
  6. course_num = len(course_list)
  7. every_score = [[score_list[j][i] for j in range(course_num)] for i in range(student_num)]
  8. every_total = [sum(every_score[i]) for i in range(student_num)]
  9. ave_course = [sum(score_list[i])/len(score_list[i]) for i in range(len(score_list))]
  10. return (every_score,every_total,ave_course)
  11. if __name__=="__main__":
  12. course_list = ["C++","Java","Servlet","JSP","EJB"]
  13. student_num = 20
  14. score_list = [[random.randint(0,100) for i in range(student_num)] for j in range(len(course_list))]
  15. for i in range(len(course_list)):
  16. print "score of every one in %s:"%course_list[i]
  17. print score_list[i]
  18. every_score,every_total,ave_one_course = score(score_list,course_list,student_num)
  19. print "\n"
  20. print "NEXT IS EVERY ONE SCORE IN EVERY COURSE:"
  21. for name in course_list:
  22. print name,
  23. print "\t"
  24. print every_score
  25. print "\n"
  26. print "every one all score:\t",every_total
  27. print "every course of average score:\t",ave_one_course

qiwsir#gmail.com (# to @)

解法 (racket 5.2.1)

  1. #lang racket
  2. (define (2d-list)
  3. (let*
  4. ([rand-100 (lambda () (random 101))]
  5. [nth-picker (lambda (n) (lambda (l) (list-ref l n)))]
  6. [average (lambda (number-list) (exact->inexact (/ (apply + number-list) (length number-list))))]
  7. [course-list (list "coreC++" "coreJava" "Servlet" "JSP" "EJB")]
  8. [score-list (for/list ([i 20]) (for/list ([j 5]) (rand-100)))]
  9. [score-by-course-list (for/list ([i 5]) (list ((nth-picker i) course-list) (map (nth-picker i) score-list)))]
  10. [score-by-student-list (for/list ([i 20]) (list-ref score-list i))]
  11. [total-by-student-list (for/list ([i 20]) (apply + (list-ref score-list i)))]
  12. [average-by-course-list (for/list ([i 5]) (list ((nth-picker i) course-list) (average (map (nth-picker i) score-list))))])
  13. (for ([i 5])
  14. (display "score of every one in ")
  15. (displayln (first ((nth-picker i) score-by-course-list)))
  16. (displayln (second ((nth-picker i) score-by-course-list))))
  17. (displayln "")
  18. (displayln "NEXT IS EVERY ONE SCORE IN EVERY COURSE: ")
  19. (displayln course-list)
  20. (for ([i 10])
  21. (displayln (list-ref score-by-student-list i)))
  22. (displayln "")
  23. (displayln "every one all score: ")
  24. (displayln total-by-student-list)
  25. (displayln "")
  26. (displayln "every course of average score: ")
  27. (displayln average-by-course-list)))
  28. ; 调用函数, 正常时应输出类似如下结果
  29. ;score of every one in coreC++
  30. ;(12 58 60 28 78 23 34 83 19 83 78 26 51 94 93 74 53 89 8 23)
  31. ;... ...
  32. ;NEXT IS EVERY ONE SCORE IN EVERY COURSE:
  33. ;(coreC++ coreJava Servlet JSP EJB)
  34. ;(12 49 75 88 68)
  35. ;(58 78 6 88 81)
  36. ;... ...
  37. ;every one all score:
  38. ;(292 311 370 241 289 250 254 258 147 232 271 170 224 248 317 286 246 270 186 212)
  39. ;
  40. ;every course of average score:
  41. ;((coreC++ 53.35) (coreJava 53.9) (Servlet 51.95) (JSP 49.6) (EJB 44.9))
  42. (2d-list)