1.判断101-200之间有多少个素数,并输出所有素数。
list1 = [ ]
conut1 = 0
for i in range ( 101 , 201 ) :
for x in range ( 2 , i) :
if i % x == 0 :
break
else :
conut1 += 1
list1. append( i)
print ( f'101-200之间有 { conut1} 个素数' )
print ( list1)
2.求整数1~100的累加值,但要求跳过所有个位为3的数。
count1 = 0
for i in range ( 101 ) :
if i % 10 != 3 :
count1 += i
print ( count1)
3.有?分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的第20个分数。
n = int ( input ( '请输入n:' ) )
count1 = 1
count2 = 2
for i in range ( n- 1 ) :
num = count1+ count2
count1 = count2
count2 = num
print ( f' { count2} / { count1} ' )
'''
# 这里注意一下当n输入1是range函数里面的值是0所以不会执行循环,会直接打印count2/count1;2/1
第一次循环:num = 2+1 ; count1 = 2 ; count2 = 3 -> 求出来的分数就是3/2
第二次循环: num = 3+2 ; count1 = 3 ; count2 = 5 -> 求出来的分数就是5/3
第三次循环: num = 5+3 ; count1 = 5 ; count2 = 8 -> 求出来的分数就是8/5
依次类推: num = count1 + count2 ; count1 = count2 ; count2 =num
'''
fen_zi = 2
fen_mu = 1
for _ in range ( n- 1 ) :
fen_zi, fen_mu = fen_zi+ fen_mu, fen_zi
print ( f' { fen_zi} / { fen_mu} ' )
4.写程序计算 n 的阶乘 n! 的结果。
n = int ( input ( '请输入n:' ) )
count1 = 1
for i in range ( 1 , n+ 1 ) :
count1 *= i
print ( count1)
5. 求1+2!+3!+…+20!的和
n = int ( input ( '请输入n:' ) )
count2 = 0
count1 = 1
for i in range ( 1 , n+ 1 ) :
count1 *= i
count2 += count1
print ( count2)
6. 写程序求表达式 a + aa + aaa + aaaa+ … 的结果,其中a是1~9的数字,求和的项数用n来控制。(a和n可以用变量来表示)
a = input ( '输入数字:' )
n = int ( input ( '输入求和项数:' ) )
sum1 = 0
b = a
for i in range ( 1 , n + 1 ) :
a = b * i
sum1 += int ( a)
print ( sum1)
a = int ( input ( '输入数字:' ) )
n = int ( input ( '输入求和项数:' ) )
sum1 = 0
for num1 in range ( 1 , n + 1 ) :
num2 = 0
for _ in range ( num1) :
num2 = num2 * 10 + a
sum1 += num2
print ( sum1)
7. 控制台输出三角形
1.根据n的值的不同,输出相应的形状
n = int ( input ( '输入n:' ) )
for row in range ( n, 0 , - 1 ) :
for _ in range ( row) :
print ( '*' , end= '' )
print ( )
n = int ( input ( '输入n:' ) )
for i in range ( n, 0 , - 1 ) :
print ( '*' * i)
2.根据n的值的不同,输出相应的形状(n为奇数)
n = int ( input ( '输入一个奇数:' ) )
for row in range ( 1 , n + 1 , 2 ) :
star = row
space = n - int ( ( row + 1 ) / 2 )
for _ in range ( space) :
print ( ' ' , end= '' )
for _ in range ( star) :
print ( '*' , end= '' )
print ( )
n = 7
for x in range ( 1 , n + 1 , 2 ) :
star_num = x
space_num = ( n - x) // 2
for _ in range ( space_num) :
print ( ' ' , end= '' )
for _ in range ( star_num) :
print ( '*' , end= '' )
print ( )
3.根据n的值的不同,输出相应的形状(内容是数字)
n = int ( input ( '输入行数:' ) )
list1 = [ ]
for row in range ( 1 , n+ 1 ) :
list1. append( row)
list2 = list1+ list1[ - 2 : : - 1 ]
star = 2 * row - 1
space = n - row
for _ in range ( space) :
print ( ' ' , end= '' )
for _ in range ( star) :
print ( list2[ _] , end= '' )
print ( )
n = 5
for num in range ( 1 , n+ 1 ) :
space_num = n - num
for _ in range ( space_num) :
print ( ' ' , end= '' )
for x in range ( 1 , num+ 1 ) :
print ( x, end= '' )
for x in range ( num- 1 , 0 , - 1 ) :
print ( x, end= '' )
print ( )
8. 小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。要把100元正好花掉,可有哪些购买结合?
for shampoo in range ( 100 // 15 ) :
for toilet_soap in range ( 100 // 2 ) :
for toothbrush in range ( 100 // 5 ) :
if shampoo * 15 + toilet_soap * 2 + toothbrush * 5 == 100 :
print ( '洗发水:' , shampoo, '香皂:' , toilet_soap, '牙刷:' , toothbrush)
9.一张纸的厚度大约是0.08mm,对折多少次之后能达到珠穆朗玛峰的高度(8848.13米)?
num1 = 8848.19 * 1000
x = 0.08
count = 0
while x <= num1:
x *= 2
count += 1
print ( f'对折 { count} 次' )
height = 0.08 / 1000
count = 0
while True :
height *= 2
count += 1
if height >= 8848.13 :
print ( '对折次数:' , count)
break
10.古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
n_2 = 1
n_1 = 1
n = int ( input ( '请输入月份:' ) )
if n == 1 or n == 2 :
print ( '第' , n, '个月兔子总数为:1' )
else :
for _ in range ( n- 3 ) :
n_1, n_2 = n_1 + n_2, n_1
print ( '第' , n, '个月兔子总数为:' , n_1+ n_2)
n = int ( input ( '请输入月份:' ) )
count1 = 1
count2 = 0
for i in range ( n) :
num = count1+ count2
count1 = count2
count2 = num
print ( f'第 { n} 个月的兔子总数为 { count2} ' )
11.将一个正整数分解质因数。例如:输入90,打印出90=2x3x3x5。
num = 90
x = 2
while True :
if num % x == 0 :
num /= x
print ( x, end= '' )
if num != 1 :
print ( 'x' , end= '' )
else :
x += 1
if num == 1 :
break
print ( )
n = int ( input ( '请输入一个任意正整数:' ) )
print ( '%d=' % n, end= '' )
while n > 1 :
for i in range ( 2 , n) :
if n % i == 0 :
n = int ( n / i)
print ( '%d*' % i, end= '' )
break
else :
print ( '质数' )
break
12. 某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。求输入的四位整数加密后的值
num1 = list ( input ( '输入四位的整数:' ) )
for i in range ( len ( num1) ) :
sum1 = ( int ( num1[ i] ) + 5 ) % 10
num1[ i] = sum1
num1[ 0 ] , num1[ 1 ] , num1[ 2 ] , num1[ 3 ] = num1[ - 1 ] , num1[ - 2 ] , num1[ 1 ] , num1[ 0 ]
print ( num1)
num = 2367
n1 = num // 1000
n2 = num // 100 % 10
n3 = num // 10 % 10
n4 = num % 10
n1 += 5
n2 += 5
n3 += 5
n4 += 5
n1 %= 10
n2 %= 10
n3 %= 10
n4 %= 10
new_num = n4* 1000 + n3* 100 + n2 * 10 + n1
print ( num, '加密后的值:' , new_num)
14. 本金10000元存入银行,年利率是千分之三。每过1年,将本金和利息相加作为新的本金。计算5年后,获得的本金是多少。
num1 = 10000
for i in range ( 5 ) :
num1 = num1 + num1* 0.003
print ( '本金是' , num1)
year = 5
capital = 10000
current_year = 1
while True :
capital *= 1 + 0.003
current_year += 1
if current_year > 5 :
break
print ( capital)
15.输入一个整数,计算它各位上数字的和。(注意:输入的整数可以是任意位)
num = 276721
sum1 = 0
while True :
sum1 += num % 10
num //= 10
if num == 0 :
break
print ( '和为:' , sum1)
num1 = input ( '输入一个整数:' )
sum1 = 0
for i in num1:
sum1 += int ( i)
print ( sum1)
16.求两个数的最大公约数和最小公倍数。(提示:公约数一定小于等于两数中的小的那个数,且能同时被两个数整除;公倍数一定大于等于两数中的大数,且是大数的倍数又能被两数中的小数整除)
num1 = 18
num2 = 15
if num1 > num2:
num1, num2 = num2, num1
max_common_divisor = num1
while True :
if num1 % max_common_divisor == 0 and num2 % max_common_divisor == 0 :
break
else :
max_common_divisor -= 1
print ( '最大公约数:' , max_common_divisor)
min_common_multiple = num2
multiple = 1
while True :
if min_common_multiple % num1 == 0 :
break
multiple += 1
min_common_multiple = num2 * multiple
print ( '最小公倍数:' , min_common_multiple)