python学习1

2020-02-27  本文已影响0人  cry15

菜鸟教程例题

在其中我有些算了一下运行时间,方法如下:

import time
start =time.clock()
# 程序
end = time.clock()
# 其中end-start就是程序运行的时间,单位是秒。
print('Running time: %s Seconds'%(end-start))
def mo_1(): 
    a = []
    for i in range(1,5):
        for j in range(1,5):
            for k in range(1,5):
                if i != j and j != k and k != i:
                    a.append(i * 100 + j *10 +k)
    print(a)
def mo_2():
    i = int(input())
    a = (1000000,600000,400000,200000,100000,0)                    
    b = (0.01,0.015,0.03,0.05,0.075,0.1)
    r = 0
    for idx in range(0,6):
        if i>a[idx]:
            r+=(i-a[idx])*b[idx]
            i=a[idx]
    print(r)
def mo_3(ans = 1):
    if ans == 1:
        # Running time: 0.015090500000000118 Seconds
        for i in range(-100,10000):
            if (i + 100) ** 0.5 == int((i + 100) ** 0.5) and (i + 100 + 168) ** 0.5 == int((i + 100 + 168) ** 0.5):
                print(i,end='')
    elif ans == 2:
        # Running time: 0.0014683999999998143 Seconds
       for i in range(1,85):
            if 168 % i == 0:
                j = 168 / i
                if  i > j and (i + j) % 2 == 0 and (i - j) % 2 == 0 :
                    m = (i + j) / 2
                    n = (i - j) / 2
                    x = n * n - 100
                    print(x,end='')

import datetime
def mo_4(ans):
    if ans == 2:
        year, month, day = [int(i) for i in input().split()]
        print("-" * 20)
        result = datetime.datetime(year, month, day)
        # 用来格式化时间,%j 用来表示天数
        print(result.strftime("%j"))
def mo_5(ans):
    print(sorted([int(i) for i in input().split()]))
def mo_6(n,ans):
    if ans == 1:
        # 当n=1000时Running time: 0.32044379999999983 Seconds
        start =time.clock()
        a = [0,1]
        for i in range(2,n):
            a.append(a[i-1] + a[i-2])
        print(a)
        end = time.clock()
        print('Running time: %s Seconds'%(end-start))
    elif ans == 2:
        # 当n=1000时Running time: 0.02545049999999982 Seconds
        start =time.clock()
        a,b = 1,1
        for i in range(n-1):
            a,b = b,a+b
        print(a)
        end = time.clock()
        print('Running time: %s Seconds'%(end-start))
    elif ans == 3:
        if n ==1 or n ==2:
            return 1
        else :
            return mo_6(n-1,ans = 3) + mo_6(n-2,ans = 3) 
def mo_7():
    for i in range(1,10):
        print()
        for j in range(1,i+1):
            print('%d * %d = %d'%(j,i,i*j),end='    ')
def mo_8():
    for x in range ( 5,-1,-1 ) :               #  生成倒计时秒数
        mystr = '倒计时' + str( x ) + '秒'       #  str()函数强制转化字符型      
        print ( mystr, end = '' )               #  end='' 不换行
        print ( "\b"*(len(mystr)*2), end = '' , flush = True )     
        time.sleep ( 1 )
def mo_9():
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
    # 暂停一秒
    time.sleep(1)
    print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
上一篇下一篇

猜你喜欢

热点阅读