python实现(九九乘法表)(水仙花)(跳七)(百钱百鸡)(冒泡排序)

# 通过for循环实现九九乘法表
def jjfrist():
    for i in range(1, 10):
        for j in range(1, i + 1):
            print("%d*%d=%d\t" % (i, j, i * j), end="")
        print()


def jjsecond():
    i = 1
    while i < 10:
        j = 1
        while j <= i:
            print("%s*%s=%s\t" % (i, j, i * j), end="")
            j += 1
        print()
        i += 1


if __name__ == '__main__':
    jjfrist()
    jjsecond()

# 水仙花
# 求1000以内的水仙花
# 水仙花算法 一个数等于它各位的立方和 例如:153 = 1*1*1 + 5*5*5 + 3*3*3


def sxhfrist():
    for a in range(1, 10):
        for b in range(10):
            for c in range(10):
                s = a * a * a + b * b * b + c * c * c
                if s == a * 100 + b * 10 + c:
                    print(a, b, c)
                    print(s)


def sxhsecond():
    for i in range(100, 1000):
        # 除以100取整(python为动态语言,除出来小数了)
        p = i // 100
        q = (i // 10) % 10
        w = i % 10
        # print(i)
        if i == p * p * p + q * q * q + w * w * w:
            # print(p,q,w)
            print(i)


if __name__ == '__main__':
    sxhfrist()
    sxhsecond()

# 跳七
# 计算1-100之间 不包含7(各位,十位)或者7的倍数


def tqfrist():
    for i in range(100):
        if i % 7 != 0 and i != 7 and i % 10 != 7 and i // 10 != 7:
            print(i)

if __name__ == '__main__':
    tqfrist()
# 百钱百鸡
# 公鸡五钱一只,母鸡3钱一只,小鸡1钱3只  花费100钱买100只鸡

def bqbjfrist():
    for i in range(21):
        for j in range(34):
            for k in range(100):
                if i + j + k == 100 and i * 5 + j * 3 + k // 3 == 100 and k % 3 == 0:
                    print(i, j, k)


# 优化版
def bqbjsecond():
    for ggj in range(21):
        for mmj in range(34):
            xxj = 100 - ggj - mmj
            if ggj * 5 + mmj * 3 + xxj // 3 == 100 and xxj % 3 == 0:
                print(ggj, mmj, xxj)


if __name__ == '__main__':
    bqbjfrist()
    bqbjsecond()

# 冒泡排序
def bubbule_sort(alist):
    for i in range(len(alist)):
        for j in range(0, len(alist) - i - 1):
            if alist[j] >= alist[j + 1]:
                alist[j], alist[j + 1] = alist[j + 1], alist[j]
    print(alist)


if __name__ == '__main__':
    alist = [90, 23, 46, 74, 23, 4, 54, 23, 45, 23, 51, 22, 3, 5]
    bubbule_sort(alist)

你可能感兴趣的:(python实现(九九乘法表)(水仙花)(跳七)(百钱百鸡)(冒泡排序))