1.读程序,总结功能
图片.png功能:幂运算,本程序生成数0~19,即2^20
2.
图片.png功能:统计1~99中3和7的公倍数中奇数的个数
3.编程实现(for和while各写一遍)
a.求1~100所有数的和、平均值
for:
int_num = avg = 0
for n in range(101):
int_num += n
print(int_num)
avg = int_num /100
print(avg)
while:
num = sm =0
while num < 100:
num += 1
sm += num
print('和:',sm,'平均数:',sm/100)
b.计算1~100间能被3整除的数的和
for:
sm =0
for num in range(1,100):
if(num % 3 == 0):
sm += num
print(sm)
while
sm = num = 0
while num >100:
num += 1
if(num % 3 == 0):
sm += num
print(sm)
c.计算1~100之间不能被7整除的数的和
for:
sm = 0
for num in range(100):
num += 1
if(num % 7 != 0):
sm += num
print(sm)
while:
sm = num =0
while num <= 100:
num += 1
if(num % 7 != 0):
sm += num
print(sm)
求斐波那契数列中第n个数 1,1,2,3,5,8,13,21,34....
num = int(input(' 你想求斐波那契第几个数啊:'))
an = 1
an_1 = 1
if(num <= 2):
print(an)
else:
for _ in range(3,num):
cn = an
an += an_1
an_1 = cn
print(an)
判断101-200之间有多少个素数,并输出所有素数。判断素数的⽅法:⽤⼀个数分别除2到sqrt(这个
数),如果能被整除,则表明此数不是素数,反之是素数
con = con_1 = 0
for num in range(101,202):
c = 2
if con == 1:
con_1 += 1
print(num - 1)
while c < num/2:
con = 1
if(num % c == 0):
con += 1
break
c += 1
print(con_1)
打印出所有的⽔仙花数,所谓⽔仙花数是指⼀个三位数,其各位数字⽴⽅和等于该数本身。例如: 153是
⼀个⽔仙花数,因为153 = 1^3 + 5^3 + 3^3
for num in range(100,1000):
if(num == (num % 10) ** 3 +(num // 10 % 10) ** 3 + (num // 100) ** 3 ):
print(num)
-
有⼀分数序列: 2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的第20个分数
fz = 2
fm = 1
for num in range(20):
cn = fz
fz += fm
fm = cn
print(fz,'/',fm)
分⼦:上⼀个分数的分⼦加分⺟ 分⺟: 上⼀个分数的分⼦ fz = 2 fm = 1 fz+fm / fz
-
给⼀个正整数,要求: 1、求它是⼏位数 2.逆序打印出各位数字
num = int(input('请输入一个正整数:'))
con = 0
while num != 0:
con += 1
w = num % 10
print(w)
num //=10
print('位数 ',con)
网友评论