Day5 课后作业
2018-12-28 本文已影响0人
LitoYu
读程序,总结程序的功能:
1
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
答:2的20次方
2
summation=0
num=1
while num<=100:
if (num%3==0 or num%7==0) and num%21!=0:
summation += 1
num+=1
print(summation)
答:1到100 中 可以被3或7整除,但是不能被21整除的数的个数
编程实现(for和while各写一遍):
1.求1到100之间所有数的和、平均值
for
sum1 = 0
for num in range(1,101):
sum1 += num
average1 = sum1 / 100
print('sum1=',sum1)
print('average1=',average1)
while
sum2 = 0
num = 1
while num <=100:
sum2 += num
num += 1
average2 = sum2 / 100
print('sum2=',sum2)
print('average2=',average2)
2.计算1-100之间能3整除的数的和
for
sum3 = 0
for num in range(1,101):
if num % 3 == 0:
sum3 += num
print('sum3=',sum3)
while
sum4 = 0
num = 1
while num <101:
if num % 3 ==0:
sum4 += num
num += 1
print('sum4=',sum4)
3.计算1-100之间不不能被7整除的数的和
for
sum5 = 0
for num in range(1,101):
if num % 7 != 0:
sum5 += num
num += 1
print('sum5=',sum5)
while
sum6 = 0
num = 1
while num < 101:
if num % 7 != 0:
sum6 += num
num += 1
print('sum6=',sum6)
稍微困难
1.1.求斐波那契数列列中第n个数的值:1,1,2,3,5,8,13,21,34....
n = float(input('n='))
print('第n个数的值:',int(1 / 5 ** 0.5 *(((1 + 5 ** 0.5 ) / 2) ** n - ((1 - 5 ** 0.5) / 2) ** n)))
第二种方法
n = input('n=')
a = 1
b = 1
if int(n) < 3:
print('n=',1)
else:
for n in range(int(n)):
c = str(a)[::]
a = b
b += int(c)
print('第n项为:',b - a)
2.求101~200的素数的个数
sum = 0
for i in range(101,201):
for g in range(2,i):
if i % g == 0:
sum += 1
break
print('素数的个数是:',100-sum)
3.打印出所有的水仙花数,所谓⽔水仙花数是指⼀一个三位数,其各位数字立方和等于该数本身。例如:153是一个水仙花数,因为153 = 1^3 + 5^3 + 3^3
for num in range(100,1000):
if int(str(num)[0]) ** 3 + int(str(num)[1]) ** 3 + int(str(num)[2]) ** 3 == num :
print(num,end=' ')
4.有一分数序列列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列列的第20个分数分子:上一个分数的分子加分母 分母: 上一个分数的分子 fz = 2 fm = 1 fz+fm / fz
fz = 2
fm = 1
for m in range(1,21):
fz = fz + fm
fm = fz - fm
print('第20个分数:',fz,'/',fm)
5.给一个正整数,要求:1、求它是几位数 2.逆序打印出各位数字
num = str(int(input('输入一个数:')))
print('它是%d位数'% len(num))
for k in num[::-1]:
print(k,end=' ')