day24

day24
1.有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和
result = 0
a = 2
b = 1
for i in range(20):
    result += a/b
    temp = b
    b = a
    a += temp
print(result)
i=0,a=2,b=1
i=1,temp=1,b=2,a=1+2
i=2,temp=2,b=3,a=3+2
i=3,temp=3,b=5,a=5+3    
方法2:根据数据规律,先生成一列数据,然后再计算
result = 0
a = [1,2]
for i in range(21):
    aa = a[i]+a[i+1]
    a.append(aa)
for k in range(len(a)):
    if k < 20:
        b = a[k+1]/a[k]
        result += b
print(result)     
34.278294788817234                           

2.画等(腰)边三角形(实心、空心)
实心:如果三角形不是很大可以用格式化输出,如果太大可以使用算法
原理:循环六次,每次循环后都换行,第一次循环打印了6个空格,接着打印了一个星号
i==2,打印了5个空格,然后打印了两个星号
i==3,   打印了4个空格,然后打印了三个星号
。。。
for i in range(1,7):
    print("\n")
    for k in range(i,7):
        print  (" ",end="")
    for j in range(i):
        print  ("*  ",end="")
空心:
for i in range(3):
    if i == 0:
        print("{1}{1}{0}{1}{1}".format("*", " "))
    if i  == 1:
        print("{1}{0}{1}{0}{1}".format("*", " "))
    if i  == 2:
        print("{0}{0}{0}{0}{0}".format("*"))

3.画倒等边三角形
for i in range(3):
    if i == 0:
        print("{0}{0}{0}{0}{0}".format("*"))
    if i  == 1:
        print("{1}{0}{1}{0}{1}".format("*", " "))
    if i  == 2:
        print("{1}{1}{0}{1}{1}".format("*", " "))

4.画直角三角形(实心、空心)
实心:
for i in range(5):
    for k in range(i+1):
        print("*",end="")
    print()

空心:
for i in range(5):
    if i == 0:
        print("{0}{1}{1}{1}".format("*", " "))
    if i  == 1:
        print("{0}{1}{0}{1}".format("*", " "))
    if i  == 2:
        print("{0}{1}{1}{0}".format("*", " "))
    if i == 3:
        print("{0}{1}{1}{1}{0}".format("*", " "))
    if i == 4:
        print("{0}{0}{0}{0}{0}{0}".format("*"))

5.用*号输出字母C的图案
for i in range(5):
    if i == 0:
        print("{0}{0}{1}{1}".format(" ", "*"))
    if i  == 1:
        print("{0}{1}{0}{0}".format(" ", "*"))
    if i  == 2:
        print("{1}{0}{0}{0}".format(" ", "*"))
    if i == 3:
        print("{0}{1}{0}{0}".format(" ", "*"))
    if i == 4:
        print("{0}{0}{1}{1}".format(" ", "*"))
posted @ 2019-12-26 08:39  绝世老中医  阅读(153)  评论(0编辑  收藏  举报