1、基础题
1、输入年份,如果输入的年是闰年打印'闰年'
否则打印'平年'
year = int(input('请输入一个年份'))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print('闰年')
else:
print('平年')
2、使用for和while循环输出 0~100内所有3的倍数。
for x in range(3, 100, 3):
print(x)
num = 0
while num < 100:
num += 1
if num % 3 ==0:
print(num)
3、使用for和while循环输出100~200内个位数或者十位数能被3整除的数。
for x in range(100, 201):
if (x % 10 != 0 and x % 10 % 3 == 0) or (x // 10 % 10 != 0 and x // 10 % 10 % 3 == 0):
print(x)
x = 100
while 100 <= x <= 200:
if (x % 10 != 0 and x % 10 % 3 == 0) or (x // 10 % 10 != 0 and x // 10 % 10 % 3 == 0):
print(x)
x += 1
4、使用for和while循环统计100~200中十位数是5的数的个数
count = 0
for x in range(100, 201):
if x //10 % 10 == 5:
count += 1
print('符合条件的个数为', count)
count = 0
x = 100
while 100 <= x <= 200:
if x //10 % 10 == 5:
count += 1
x += 1
print('符合条件的个数为', count)
5、使用for和while循环打印50~150中所有能被3整除但是不能被5整除的数
for x in range(50, 151):
if x % 3 == 0 and x % 5:
print(x)
x = 50
while x <= 150:
if x % 3 == 0 and x % 5:
print(x)
x += 1
6、使用for和while循环计算50~150中所有能被3整除但是不能被5整除的数的和
total = 0
for x in range(50, 151):
if x % 3 == 0 and x % 5:
total += x
print('结果为:', total)
x = 50
total = 0
while x <= 150:
if x % 3 == 0 and x % 5:
total += x
x += 1
print('结果为:', total)
2、进阶题
1.使用循环计算1*2*3*4*...*10
的结果。
count = 1
for x in range(1, 11):
count *= x
print('结果是:', count)
2.统计100以内个位数是2并且能够被3整除的数的个数。
x = 0
count = 0
while x <= 100:
if x % 10 == 2 and x % 3 == 0:
count += 1
x += 1
print('个数是:', count)
3.输入任意一个正整数,求他是几位数?
注意: 这儿不能使用字符串,只能用循环
num = int(input('请输入一个正整数'))
x = 10
count = 1
while num // x:
x *= 10
count += 1
print('这个数是', count, '位数')
4.打印出所有的水仙花数,所谓水仙花数是指一个三位数,其各位数字⽴方和等于该数本身。例如:153是
⼀个⽔仙花数,因为 1³ + 5³ + 3³
等于 153。
for x in range(100, 1000):
hundreds = x // 100 # 百位
tens_digit = x // 10 % 10 # 十位
units = x % 10 # 个位
if hundreds**3 + tens_digit**3 + units**3 == x:
print(x)
3、挑战题
1.判断指定的数是否是素数(素数就是质数,即除了1和它本身以外不能被其他的数整除的数)
num = int(input('请输入一个整数'))
if num == 1 or num == 2:
print(num, '是素数')
count = 0
for x in range(2, num // 2 + 1):
if num % x == 0:
count += 1
if count == 0:
print(num, '是素数')
else:
print(num, '不是素数')
2.求斐波那契数列列中第n个数的值:1,1,2,3,5,8,13,21,34… (这儿的n可以是任意正整数,可以通过输入来确定)
n = int(input('请输入任意正整数n:'))
Fibonacci_n = 0
Fibonacci_m = 1 # Fibonacci_n的后一位
for x in range(0, n):
Fibonacci_n, Fibonacci_m = Fibonacci_m, Fibonacci_n + Fibonacci_m
print(Fibonacci_n)
3.输出9*9口诀。 程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
for i in range(1, 10):
print(end='\n')
for j in range(1, i+1):
print(i, '*', j, '=', i*j, end=' ')
4.这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?(可以直接使用穷举法)
for big in range(1, 34):
for mid in range(1, 50):
if big*3 + mid*2 + (100-big-mid)*0.5 == 100:
print('大马:', big, '中马:', mid, '小马:', 100-big-mid)