zoukankan      html  css  js  c++  java
  • Python 初体验(八)

    编写简单脚本:

    you draw n random uniformly distributed numbers, where n is given on the command line, and compute the average of these numbers.

    the lesson learned from this little prctice can be concluded as:

    1. generation of random number
    2. read parameter from command line with sys.argv
    3. application of regular expression in Python
    import sys
    import random
    import re
    if len(sys.argv) != 2:
        print 'invalid parameter input'
        sys.exit()
    pattern = re.compile(r'^\d+$')
    match = pattern.match(sys.argv[1])
    if match:
        b=[]
        a = int(sys.argv[1])
        while(a != 0):
            b.append(random.uniform(-1,1))
            print '%.4f' % b[-1]
            a -= 1
        average = sum(b)/len(b)
        print 'Average: %.4f' % average
    else:
        print 'invalid parameter input'
        sys.exit()
    
  • 相关阅读:
    uva 1374 快速幂计算
    uva 1343 非原创
    uva 11212
    uva 10603
    路径寻找问题……!
    bzoj 1008: [HNOI2008]越狱
    bzoj 1010: [HNOI2008]玩具装箱toy
    dp斜率优化小计
    bzoj 1002[FJOI2007]轮状病毒
    hihocoder #1114
  • 原文地址:https://www.cnblogs.com/bovine/p/2264623.html
Copyright © 2011-2022 走看看