- 读程序
numbers=1
for i in range(0,20):
numbers*=2
print(numbers)
"""
numbers = 1
i = 0,1,2,3...19 (20次)
i = 0; numbers*=2 -> numbers = numbers * 2 = 1*2=2 2**1
i = 1; numbers*=2 -> numbers = numbers * 2 = 2*2 2**2
i = 2; numbers*=2 -> numbers = numbers * 2 = 2*2*2 2**3
i = 3; numbers = 2**4
...
i = 19
numbers = 2**(i+1)
求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)
"""
summation = 0
num = 1
(num:1~100, 100次)
while 1<=100; if False; num += 1, num = 2
while 2<=100; if False; num += 1, num = 3
while 3<=100; if True, summation += 1, summation = 1; num+=1, num = 4
统计1~100中能够被3或者7整除但是不能同时被3、7整除的数的个数
"""
- 求1到100所有数的和、平均值
for
sum1 = 0.0
for x in range(1, 101):
sum1 += x
print('和:',sum1, '平均值:', sum1/100)
# while
num = 1
sum1 = 0
while num <= 100:
sum1 += num
num += 1
print('和:',sum1, '平均值:', sum1/100)
2.计算1-100之间能3整除的数的和
sum1 = 0
for x in range(3, 101, 3):
sum1 += x
print(sum1)
sum1 = 0
for x in range(1,101):
if x % 3 != 0:
continue
sum1 += x
# if x % 3 == 0:
# sum1 += x
print(sum1)
num = 3
sum1 = 0
while num <= 100:
sum1 += num
num += 3
- 求斐波那契数列列中第n个数的值:(0),1,1,2,3,5,8,13,21,34....
规则: 从第二个数开始,后面的数的值就是这个数前两个数的和
current = 1 # 当前数
pre_1 = 1 # 前一个数
pre_2 = 0 # 前两个数
n = 7
for x in range(n-1):
current = pre_1 + pre_2
pre_2 = pre_1
pre_1 = current
print(current)
"""
"""
# 求指定数列中第n个数: 2, 5, 10, 17, 26,.....
# n**2 = 1
n = 4
print(n**2+1)
# while True:
# value = input(':')
# if value == '1':
# break
# num = 1
# sum1 = 0
# while True:
# sum1 += num
# num += 1
# if sum1 >= 10000:
# break
# print(num, sum1, sum1-num)
- 判断101-200之间有多少个素数,并输出所有素数。
素数(质数): 除了1和它本身,不能再被其他的数整除
"""
思路: 1.先取出101-200中的每一个数- 看从2~ N-1之间有没有一个数能够被N整除,只要有一个就说明N不是素数; 一个都没有才说明N是素数
"""
- 看从2~ N-1之间有没有一个数能够被N整除,只要有一个就说明N不是素数; 一个都没有才说明N是素数
for num in range(101, 201):
for x in range(2, num):
if num % x == 0:
# print(num, '不是素数')
break
else:
print(num, '是素数')
print('===============方法二===============')
# 用num取出101~200
for num in range(101,201):
# 假设取出来的数是素数
is_prime = True
# 判断取出来的数在2~这个数-1之间能不能找到一个能整除的数
for x in range(2, num-1):
if num % x == 0:
# 如果有一个能够被整除,说明当前数不是素数
is_prime = False
break
# 如果假设没有被推翻,说明是素数
if is_prime:
print(num, '是素数!')
print('==============方法三================')
num = 101 # num = 101 ~ 200
while num <= 200:
x = 2 # x = 2 ~ num-1
while x<=num-1:
if num % x == 0:
break
x += 1
else:
print(num, '是素数!')
num += 1
- 有⼀分数序列:(1/1),2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的第20个分数
分子: 前一个分数的分子+前一个分数的分母
分母: 前一个分数的分子
思路: 用两个变量保存前一个分数的分子和分母, 然后根据规则不断更新分子和分母的值
fen_zi = 1
fen_mu = 1
n = 4
for x in range(n):
# 方法一(python)
fen_zi, fen_mu = fen_zi + fen_mu, fen_zi
# 方法二(C/Java)
# temp = fen_zi
# fen_zi = fen_zi + fen_mu
# fen_mu = temp
print(fen_zi,'/', fen_mu)
# 交换两个数
a = 1
b = 2
c = 3
d = 5
a, b, c, d = d, a, b, c
# t = a
# a = b
# b = t
print(a, b)
- 给⼀个正整数,要求:1、求它是几位数 2.逆序打印出各位数字
解题思路: 看一个正整数是几位数,就看这个数整除几次10以后会变成0
123//10 = 12 12//10=1 1//10 = 0
n = 12389
count = 0
while True:
print(n%10)
n //= 10
count += 1
if n == 0:
break
print(count)
1.不断的从控制台输入年龄,根据年龄输出不同的提示(例如:老年人,青壮年,成年人,未成年,儿童)
输入'q',就退出
# while True:
# value = input('请输入年龄,输入q结束:')
# if value == 'q':
# break
# age = int(value)
# if age <= 12:
# print('儿童')
# elif age < 18:
# print('未成年')
# elif age <= 50:
# print('青壮年')
# else:
# print('老年')
# 2.计算5的阶乘 5!的结果是
# 5! = 5*4*3*2*1
print('==============第二题============')
n = 5
sum1 = 1
for x in range(1, n+1):
sum1 *= x
print(sum1)
3.求1+2!+3!+...+20!的和
思路: 1. 先分别取出1~20 2.求每次取出的数的阶乘
n = 20
sum2 = 0 # 保存最后的和
for num in range(1, n+1):
sum1 = 1 # 保存每个数的阶乘
for x in range(1, num+1):
sum1 *= x
sum2 += sum1
print(sum2)
练习: 外面的for循环循环第5次的时候, 里面的循环第3次, sum1的值和sum2的值分别是多少。写出执行过程
# sum1 - 6, sum2 - 33
"""
num = 1,2,3,4,5...20
num = 1,
sum1 = 1
x = 1
x = 1: sum1 = 1
sum2 += sum1 = 1
num = 2
sum1 = 1
x = 1, 2
x = 1, sum1 = 1
x = 2, sum1 = 1*2
sum2 = 1 + 2 = 3
num = 3
sum1 = 1
x = 1,2,3
x = 1, sum1 = 1
x = 2, sum1 = 2
x = 3, sum1 = 6
sum2 = 3 + 6 = 9
num = 4
sum1 = 1
x = 1,2,3,4
x = 1, sum1 = 1
x = 2, sum1 = 2
x = 3, sum1 = 6
x = 4, sum1 = 24
sum2 = 9 + 24 = 33
num = 5
sum1 = 1
x = 1,2,3,4,5
x = 1, sum1 = 1
x = 2, sum1 = 2
x = 3, sum1 = 6
"""
5.循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果
print('==================第五题==================')
# sum1 = 0
# while True:
# value = int(input('请输入一个数:'))
# if value == 0:
# break
# sum1 += value
# print(sum1)
练习: 猜数字游戏
随机产生一个数,用户不断输入数字,直到输入的数字和产生的随机数一样,游戏就结果。
游戏过程中,根据输入的数字做相应的提示(大了,小了)
random是python提供的和随机数相关的模块
import random
"""
random.randint(M,N) - 产生一个M~N的随机整数
"""
# print('===============欢迎来到猜数字游戏=================')
# num = random.randint(0, 100)
# count = 0
# while True:
# value = int(input('请输入:'))
# count += 1
# if value == num:
# print('🌺恭喜你猜对了!!🌺, 总共猜了', count, '次!')
# break
# elif value > num:
# print('😭大了, 再小点儿')
# else:
# print('😿小了,再大点儿')
5.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
3, 33, 333
"""
3 = 0*10 + 3
33 = 30 + 3 = 3*10 + 3
333 = 330+3 = 33*10 + 3
3333 = 3330+3 = 333*10 + 3
规律: 当前项 = 前一项*10 + 3
"""
n = 5 # 决定总共有多少项
a = 2 # 每一项上的每一位是多少
sum1 = 0
num = 0 # 保存求和的每一项
for x in range(6):
num = num * 10 + a
print(num)
sum1 += num
print(sum1)
补充: print的定制
可以在print括号的最后通过end来设置一个print结束后的效果;还可以通过sep来设置多个数据之间的间隔
6.根据n的值的不同,输出相应的形状
print('=========补充==========')
print('*', end=',')
print('*')
print(10, 20, sep='+++++', end=' ')
print(100)
"""
n = 5
*****
****
***
**
*
n = 4
****
***
**
*
"""
n = 7
for x in range(n, 0, -1):
# print('*' * x)
for _ in range(x):
print('*', end='')
print()
b.根据n的值的不同,输出相应的形状(n为奇数)
"""
n = 5
*
***
*****
1: 1星 2空 1+2*2 = 5
2: 3星 1空 3+1*2 = 5
3: 5星 0空 5+0*2 = 5
n = 7
* 1 3s 1+3*2 = 7
*** 3x 2s 3+2*2 = 7
***** 5x 1s 5+1*2 = 7
******* 7x 0s 7+0*2 = 7
规律: 每一行星个数+空格数的2倍 == n
"""
n = 11
for star in range(1, n+1, 2):
space = int((n - star) / 2)
# 打印空格
for _ in range(space):
print(' ', end='')
# 打印*
for _ in range(star):
print('*', end = '')
print()
"""
n = 5
*
***
*****
***
*
n = 7
*
***
*****
*******
*****
***
*
"""
10.这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
for small in range(2, 101, 2):
for big in range(1, 34):
for middle in range(1, 51):
if (small + big + middle == 100) and (small/2 + big*3 + middle * 2 == 100):
print('大:',big,'中:', middle, '小:', small)