最近需要一个入学选号的程序,通过Python可以快速的实现。xueWeiShu 表示学位数,baoMingShu 表示报名的总人数。
import random xueWeiShu = 100 baoMingShu = 500 s = [] while (len(s) < xueWeiShu): x = random.randint(1, baoMingShu) if x not in s: s.append(x) s.sort() print(s)
代码可以在下面的网站直接得到结果。
https://c.runoob.com/compile/6
经过在网上搜索,发现有更简单的代码能实现
import random xueWeiShu = 100 baoMingShu = 500 s=random.sample(range(baoMingShu+1), xueWeiShu) s.sort() print(s)