设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
楼主: crossin先生
打印 上一主题 下一主题

【每日一坑 1】 随机取数

  [复制链接]

1

主题

0

好友

50

积分

注册会员

Rank: 2

楼主
发表于 2014-6-23 23:56:56 |显示全部楼层
  1. #!/usr/bin/env python
  2. #coding:cp936

  3. from random import randint

  4. print '*'*6+'从1~n中,随机取m个数'+'*'*6
  5. while(True):
  6.     n=int(raw_input('输入n\n'))
  7.     m=int(raw_input('输入m\n'))
  8.     result=[]
  9.     if m>n or m<1:
  10.         print '注意:1<=m<=n\n重新开始\n'
  11.     else:
  12.         i=1
  13.         while i<=m:
  14.             random=randint(1,n)
  15.             if random not in result:
  16.                 result.append(random)
  17.                 i+=1
  18.         print tuple(result)
  19.         break
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-5-4 19:04 , Processed in 0.016479 second(s), 22 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部