基础
'''
读程序,总结程序的功能:
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
'''
打印2^20
'''
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)
'''
小于等于一百的整数中,遇到可以被3、7整除,但不能被21整除的,summation加1,最后打印summation
'''
编程实现(for和while各写⼀一遍):
- 求1到100之间所有数的和、平均值
num = 0
for n in range(1,101):
num += n
n += 1
print(num)
print(num/100)
num1 = 0
n1 = 1
while n1 < 101:
num1 += n1
n1 += 1
print(num1)
print(num/100)
- 计算1-100之间能3整除的数的和
num3 = 0
for n2 in range(1,101):
if n2 % 3 == 0:
num3 += n2
n2 += 1
else:
n2 += 1
print(num3)
num4 = 0
n3 = 1
while n3 < 101:
if n3 % 3 ==0:
num4 += n3
n3 += 1
else:
n3 += 1
print(num4)
- 计算1-100之间不不能被7整除的数的和
num = 0
for n in range(1,101):
if n % 7 != 0:
num += n
n += 1
else:
n += 1
print(num)
num1 = 0
n1 = 1
while n1<101:
if n1 % 7 != 0:
num1 += n1
n1 += 1
else:
n1 += 1
print(num1)
'''
稍微困难
1. 求斐波那契数列列中第n个数的值:1,1,2,3,5,8,13,21,34....
a = 1
b = 1
n = 0
while n <
2. 判断101-200之间有多少个素数,并输出所有素数。判断素数的⽅方法:用一个数分别除2到sqrt(这个
数),如果能被整除,则表明此数不不是素数,反之是素数
num = 0
for n in range(101,201):
for x in range(2,n):
if n % x == 0:
break
else:
num += 1
print(n)
print(num)
3. 打印出所有的⽔水仙花数,所谓⽔水仙花数是指⼀一个三位数,其各位数字⽴立⽅方和等于该数本身。例例如:153是
⼀一个⽔水仙花数,因为153 = 1^3 + 5^3 + 3^3
n = 100
while n < 1000:
x = n //100
y = (n // 10) %10
z = n%10
if n == x3+y3+z**3:
print(n)
n += 1
else:
n += 1
4. 有⼀一分数序列列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列列的第20个分数
分子:上一个分数的分子加分母 分母: 上一个分数的分子 fz = 2 fm = 1 fz+fm / fz
a = 2
b = 1
for n in range(1,20):
c = a
a = a + b
b = c
print(a+b)
print(a)
5. 给⼀一个正整数,要求:1、求它是⼏几位数 2.逆序打印出各位数字
n = 123456
print("共有" + str(len(str(n))) + "位")
if len(str(n)) > 1:
while n > 9:
a = n % 10
n -= a
n /= 10
print(int(a))
else:
print(int(n))
else:
print(a)