自己练习python小代码

#题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少

for i in range(1,5):
    for j in range(1,5):
        for x in range(1,5):
            if i != j and j!= x and i!=x:
                print(i,j,x)

#乘法口诀表

for m in range(1,10):
    for n in range(1,10):
        if m>=n:
            print('%s×%s=%s'%(m,n,m*n),end=' ')
    print()
for i in range(1, 10):
    for j in range(1, i + 1):
        print(("%sx%s=%s" % (i, j, i * j)).split(" "), end="")
    print()

#斐波那契数列

def fib(n):
    if n==1 or n==2:
        return 1
    return fib(n-1)+fib(n-2)

#输入三个整数x,y,z,请把这三个数由小到大输出。

L = []
for i in range(3):
    x = input("")
    L.append(x)
L.sort()
print(L)

#常用的时间格式

import time

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()))

#二分查找:

def bin_search_rec(data_set, value, low, high):
    if low < high:
        mid = (low + high) // 2
        if data_set[mid] == value:
            return mid
        elif data_set[mid] > value:
            return bin_search_rec(data_set, value, low, mid - 1)
        else:
            return bin_search_rec(data_set, value, mid + 1, high)
    else:
        return None
 
def check(a, x):
    n = len(a)
    m = n // 2
    if n<1:
        return False
    if a[m] > x:
        return check(a[0:m],x)
    elif a[m] < x:
        return check(a[m+1:],x)
    else:
        return m

#冒泡排序法:

def bubble_sort(li):
 
    for j in range(len(li)-1):
        for i in range(1, len(li)):
            if li[i] > li[i-1]:
                li[i], li[i-1] = li[i-1], li[i]
 
    return li

 

你可能感兴趣的:(Python)