1.读程序,总结程序的功能:
"""
numbers = 1
for in range(1,20):
numbers*=2
print(numbers)
"""
计算2的20次方的值
"""
代码解析:numbers = 1
i=0-19,20次
i=0,numbers=2 =2 2(0+1)
i=1, numbes=2=22 2(1+1)
........
i=19,numbers=219 2*(19+1)
"""
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)
.统计0-100以内能被3或者7整除,但是不能被21整除的数的个数!
"""
3.求1到100之间所有数的和,平均值
""" for 循环
sum1=0
for num in range(1,100):
sum1+=num
print("和",sum1,"平均值",sum1/100)
"""
"""while 循环
num1=1
sum1=0
while num1 in range(1,100):
sum1+=num1
num1+=1
print("和",sum1,"平均值",sum1/100)
4. 计算1-100之间能3整除的数的和
sum1=0
for num in range (1,101,3):
sum1+=num
print("和",sum1)
2
"""sum1=0
for num in range(101):
if num%3 ==0:
print(sum1)
while num <=100:
sum1+=num
num+=3
print("和",sum1)
"""
5. 计算1-100之间不能被7整除的数的和
"""
sum1=0
num=1
for x in range (1,100):
if num%7!=0:
sum1+=num
print(sum1)
"""
6. 求斐波那契数列中第n个数的值:1,1,2,3,5,8,13,21,34...
规律;从第三个数开始,后面的数都是前两个我数的和
"""
pre_1=1 #前第一个数
pre_2=1 #前第一个数
n=6 #求第几个数
current = pre_1+ pre_2 #当前数
if n == 1 or n == 2 :
current = 1
从第三个数开始
for x in range (n-2):
current = pre_1 +pre_2
pre_2 = pre_1
pre_1 = current
"""
7. 判断101-200之间有多少个素数,并输出所有素数。判断素数的##⽅法:⽤⼀个数分别除2到sqrt(这个数)#如果能被整除,则表明此#数不是素数,反之是素数
素数又叫质数:除了1和他本身以外不能被数整除
"""
count = 0
for num in range(101,201):
print ("++++++",num)
判断取出来的数是否是素数
for x in range (2,num):
if num%x == 0:
# print (num,"不是素数")
break
else:
print(num,"素数")
count += 1
print("101-200之间素数的个数",count)
方法2
count = 0
for num in range (101,200):
Flag = True #假设取出来的是素数
for x in range(2,num):
if x % 2 == 0:
Flag=False #如果在2和num-1之间有一个数能被num整除,假设不成立
if Flag:
count += 1
print(num,"素数")
print("101-200素数的个数",count)
"""
8打印出所有的⽔仙花数,所谓⽔仙花数是指⼀个三位数,其各位数字⽴⽅和等于该数本身。例如:153是
个⽔仙花数,因为153 = 1^3 + 5^3 + 3^3
"""
for num in range(100,1000):
ge = num%10
shi= num//10%10
bai= num//100
if num == ge3+shi3+bai**3:
print(num,"是水仙花数")
9. 有⼀分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的第20个分数
分子:下一个数的分子 是上一个数的分子+分母
分母:下一个分数的分母 ==上一个数的分子
fen_zi = 1
fen_mu = 1
for _ in range (20):
fen_zi,fen_mu=fen_zi+fen_mu,fen_zi
print(fen_zi,"/",fen_mu)
10. 给⼀个正整数,要求:1、求它是⼏位数 2.逆序打印出各位数字
"""
num=788321
count = 0
while True:
print(num%10,end="")
num//=10
count +=1
if num ==0:
break
print()
print(count,"位数")
方法2
num=788123
print("位数",len(str(num)))
print("逆序",str(num)[::-1])
"""
11.计算5的阶乘 5!的结果是
"""
sum1=1
n=5
for x in range (1,n+1):
sum1*=x
print (sum1)
"""
12.求1+2!+3!+...+20!的和 1.程序分析:此程序只是把累加变成了累乘for
"""
sum1=0#保存每个数的和
for num in range(1,21):
sum2=1#保存当前取出来的数的阶层
for x in range(1,num+1):
sum2*=x
sum1+=sum2
print(sum1)
"""
13.循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果
"""
sum1=0
while True:
num = int (input("请输入一个数字"))
sum1+=num
if num == 0:
break
print(sum1)
"""
14.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如#2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘#控制。 1.程序分析:关键是计算出每一项的值。
"""
num=4 #(数字每一位上的数)
n=6 #(求和的项数)
sum1=0 #(和)
num1=num #(个位数)
for _ in range(n):
print(num)
sum1+=num
num=num*10+num1
print("和",sum1)
"""
练习
"""
num=0 #每一项的数字
sum1=0
for x in range(0,10):
num=num*10+x
print(num)
sum1 += num
print(sum1)
"""
15.输入三个整数x,y,z,请把这三个数由小到大输出。
"""
x = 9
y = 20
z = 1
最大值
max1 = x
if y > max1:
max1 = y
if z > max1:
max1 = z
print(max1)
"""
最小值
"""
min1 = x
if y < min1:
min1 = y
if z < min1:
min1 = z
print(min1)
print(min1)
print('中间:', x+y+z - max1 - min1)
print(max1)
"""
16.控制台输出三角形
"""
n=10
控制行数
for x in range(n):
#控制一行的数量
for y in range(n-x):
print("",end="")
print( )#换行
"""
b.根据n的值的不同,输出相应的形状(n为奇数
print("第5题=====================")
"""n = 5 n = 7
*
*******
"""
"""n=7
for count in range (1,n+1,2):
space = int((n - count)/2)
print(" "space,end="")
print(""*count)
"""
练习
"""
n = 5
"""
print("第6题=====================")
17.输出9*9口诀。 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列
"""
for i in range(1,10):
for j in range(1,i+1):
print(j,"x",i,"=",i*j,end=" ",sep="")
print()
"""
18.这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
"""
for big in range(1,100//3):
for mid in range(1,100//2):
for small in range (2,100,2):
if big +mid +small==100 and big3+mid2+small/2==100:
print("大马",big ,"中马",mid,"小马",small)
"""
19.我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的: 5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现。
"""
for cock in range (1,100//5):
for hen in range (1,100//3):
for chick in range(3,100,3):
if cock + hen + chick == 100 and cock5+hen3+chick/3==100:
print("公鸡",cock,"母鸡",hen,"小鸡",chick)
"""
20.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元整好花掉,可如有哪些购买结合?
"""
for a in range (1,100//15):
for b in range (1,100//2):
for c in range (1,100//5):
if a + b + c == 100 and a15 + b2 + c*5 == 100:
print("洗发水",a ,"香皂",b,"牙刷",c)
"""