免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2866 | 回复: 0
打印 上一主题 下一主题

python习题:得分排行 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-09 11:59 |只看该作者 |倒序浏览

               
#!/usr/bin/env python
#得分排名
import random;
def score_rank(scores):
    ranks = [0] * 102
    for score in scores:
        ranks[score] += 1
    ranks[101] = 1
    for i in range(len(ranks)-2,0,-1):
        ranks += ranks[i+1]
    return ranks;
if __name__ == "__main__":
   #create a random score list
   scores = []
   for x in range(40):
       scores.append(random.randint(1,100))
   ranks = score_rank(scores)
   #print result
   scores.sort()
   for x in scores:
       print("分数:{0}\t排名:{1}".format(x,ranks[x+1]))
执行结果:
>>>
分数:4    排名:40
分数:7    排名:39
分数:10    排名:38
分数:12    排名:37
分数:13    排名:36
分数:21    排名:35
分数:22    排名:34
分数:25    排名:33
分数:30    排名:32
分数:31    排名:31
分数:32    排名:30
分数:35    排名:29
分数:36    排名:27
分数:36    排名:27
分数:37    排名:24
分数:37    排名:24
分数:37    排名:24
分数:41    排名:23
分数:42    排名:22
分数:44    排名:21
分数:45    排名:19
分数:45    排名:19
分数:47    排名:18
分数:50    排名:17
分数:51    排名:16
分数:55    排名:15
分数:61    排名:14
分数:67    排名:13
分数:68    排名:12
分数:69    排名:11
分数:72    排名:10
分数:73    排名:9
分数:74    排名:8
分数:75    排名:7
分数:76    排名:6
分数:83    排名:5
分数:85    排名:4
分数:93    排名:2
分数:93    排名:2
分数:95    排名:1
>>>
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/23094/showart_2089686.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP