1.读程序功能。
1 2 4 8 16
2.summation=43
(33+14-4=43)
编程实现(for和while各写⼀一遍):
a.求1到100之间所有数的和、平均值:
# for 循环求和===============
num =0 #值
n = 0 #次数
for n in range(101):
num += n
print(num)
#while求和========================
num1 = 0
n1 = 0
while n1 <= 100:
num1 += n1
n1 += 1
print(num1)
#for循环:求平均值=====================
num =0
n = 0
for n in range (101):
num += n
print(num / 2)
#while循环求平均值===================
num=0
n=0
while n<=100:
num+=n
n+=1
print(num/2)
2.计算1-100之间能3整除的数的和
# for循环:==========================
num =0
n = 0
for n in range (101):
if n % 3 ==0:
num += n
print(num)
# while循环:====================
num = 0
n = 0
while n <=100:
if n % 3 ==0:
num += n
n += 1
print(num)
3.计算1-100之间不不能被7整除的数的和
for循环:
num =0
n = 0
for n in range (101):
if n % 7 !=0:
num += n
print(num)
print("============================================")
while循环:
num = 0
n = 0
while n <=100:
if n % 7 != 0:
num += n
n += 1
print(num)
稍微困难
1.求斐波那契数列列中第n个数的值:1,1,2,3,5,8,13,21,34....
2.判断101-200之间有多少个素数,并输出所有素数。判断素数的⽅方法:⽤用⼀一个数分别除2到sqrt(这个数),
如果能被整除,则表明此数不不是素数,反之是素数
num=0
n=102
for n in range (101, 200):
if not (n % 2 == 0 or n % 3 == 0 or n % 5 ==0 or n % 7 ==0 or n % 7 ==0 ):
print(n)
num += n
print(num)
3.打印出所有的⽔水仙花数,所谓⽔水仙花数是指⼀一个三位数,其各位数字⽴立⽅方和等于该数本身。例例如:153是
⼀一个⽔水仙花数,因为153 = 1^3 + 5^3 + 3^3
4.有⼀一分数序列列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列列的第20个分数分⼦子:上⼀一个分数的分⼦子加分⺟母 分⺟母: 上⼀一个分数的分⼦子 fz = 2 fm = 1 fz+fm / fz
5.给⼀一个正整数,要求:1、求它是⼏几位数 2.逆序打印出各位数字
num = 12345 #itn(input("请输入一个正整数: "))
n=1
while True:
num1 = (num / 10**n)
print(num1 % 10*n)
n += 1
if num1 <= 1:
break