作业练习

#求圆的半径跟周长 圆周率3.14

r = int(input('Please enter the radius>>>'))
c = 1
while c:
	 arce = 3.14*r*r
     perimeter = 2*3.14*r
     c -= 1
     print('The area is:', arce)
     print('The perimeter is:', perimeter)

#输入两个数 比较大小 升序打印

c  = 1
while c:
    c -= 1
    one = input('The first number:')
    two = input('The second number')
    if one > two:
        print(one, two)
    else:
        print(two, one)

输入若干个整数 打印最大值 如果为0就退出

m = int(input('Please one number'))
while 1:
    c = int(input('Please enter the number'))
    if c == 0:
        break
    else:
        n = c
        if n > m :
            m = n
        print('Max=',m)

输入n个数 求每次的算数平均数

n = 0
m = 0
for i in range(5):
    nums = int(input('Please a number'))
    if nums == 0:
        break
    n += 1
    m += nums
    avg = m/n
    print(avg)

打印边长为n的正方形

n = int(input('Please output side length'))
for i in range(n-1):
    if i == 0:
        print('*'*n)
    else:
        print('*'+' '*(n-2)+"*")
print('*'*n)

求100以内所有奇数和

s = 0
for i in range(1,101,2):
    s = s+i
print(s)

判断学生成绩

results = int(input('Please enter your score:'))
if results >= 70:
    if  90 > results >= 80:
        print('B')
    elif results >= 90:
        print('A')
    else:
        print('C')
else:
    if results >= 60:
        print('D')
    else:
        print('E')

求1到5的阶乘

k = int(input('Please enter a number:'))
nums = 1
count = 0
for i in range(1,k+1):
    nums *= i
    count += nums
print('阶乘=',nums)
print('阶乘和=',count)

给一个数 判断是否为质数

c = int(input('Please enter a number:'))
if c == 2:
    print(c, 'Yes')
elif c % 2 == 0:
    print(c, 'No')
if c % 2 != 0:
    i = 2
    while i < c:`在这里插入代码片`
        if c % i == 0:
            print(c,'No')
            break
        i += 1
    else:
        print(c,'Yes')

打印菱形

n = int(input('Please a number'))
if n % 2 == 0:
    n += 1
for i in range(-int(n/2),int(n/2+1)):
    print(' '*abs(i) + '*' * (n-2*abs(i)))

打印对顶三角形

n =7
e = n//2
for i in range(-e, n-e):
    line = '*' * (2 * abs(i) + 1)
    print('{:^{}}'.format(line,n))

打印闪电

n = 7 
e = n // 2
for i in range(-e, e+ 1):
    if i < 0:
        print(' ' * (-i) + '*' * (4+i))
    elif i == 0:
        print('*' * n)
    else:
        print(' '*3 + '*'*(4-i))

解决猴子吃桃问题

peach = 1
for _ in range(9):
    peach = (peach + 1) * 2
print(peach)

打印99乘法表

for i in range(1,10):
    for j in range(1,i+1):
        print('{}*{}={}{}'.format(j,i,i*j,' '*3 if j >1 and i*j <10 else ' '*2),end = ' \n' if j == i else '')

打印100以内的斐波那契数列

m = 0
n = 1
print(1)
while 1:
    sum = m + n
    if sum >100:
        break
    m = n
    n = sum
    print(sum)

打印斐波那契第101项

# m = 0                             a = 0
# n = 1                             b = 1
# sum = 0                           count = 1
# for i in range(1,101):            while 1:
#     sum = m + n                       c = a + b
#     m = n                             if count == 100:
#     n = sum                               print(c)
# print(sum)                                break
#                                         count += 1
#                                         a, b = b, c

求10万以内的素数 基本版本

print(2)
for i in range(3,1000,2):
    if i > 10 and i % 5 ==0:
        continue
    else:
        for j in range(2,int(i**0.5+1)):
            if i % j == 0:
                break
        else:
            print(i)

求10万以内的素数

开方加6n+1 6n-1 (孪生定理)算法

    start = datetime.datetime.now()
    n = 100000
    count = 3
    x = 7
    step = 4
    while x < n:
        if x % 5 != 0:
            for i in range(3,int(x**5)+1,2):
                if x % i == 0:
                    break
            else:
                count += 1
        x += step
        step  = 4 if step == 2 else 2
    delta = (datetime.datetime.now() - start).total_seconds()
print(count)
print(delta)

杨辉三角 补0法

l = int(input('Please one number:'))
m = [1]
n = []
s = []
for i in range(0,l):					#循环用户输入的次数 这是列
    n = m.copy()						#copy一份m 给n
    n.append(0)							#在n后面追加一个0   目前n = [1,0]
    m.clear()							#清空一下m  每次都要清空
    for j in range(i+1):				#这里循环到用户输入+1的地方  这是行
        m.append(n[j-1] + n[j])			#第一次进来用n的负1索引对应的数加上索引为0的数追加到m这个空列表里
    print(m)							#凑齐一行列表才打印

杨辉三角 单行实现

n = 9
line = [1] * n # 一次性开辟好于次次追加
print(line)

for i in range(n): # i
    offset = n - i
    swap = 1
    for j in range(1, i//2+1): # i=3 range(1, 2) # 1
				#val = swap + line[j] #line[j-1] + line[j]
 			    #swap = line[j] # 3
				#line[j] = val # 4
        line[j], swap = swap + line[j], line[j]			#等价上面三行代码
        if i != 2*j:
            line[-j-offset] = line[j]
    
    print(line[:i+1])

你可能感兴趣的:(作业,作业)