Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54279
  • 博文数量: 29
  • 博客积分: 1235
  • 博客等级: 中尉
  • 技术积分: 290
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-20 21:15
文章分类

全部博文(29)

文章存档

2010年(7)

2009年(22)

我的朋友

分类: Python/Ruby

2009-11-09 11:59:37



#!/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[i] += 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
>>>

阅读(500) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~