关于Python的几个有趣小题
春节前后公司也不忙,在网上找了几个python小题目做了下,记录一下下,方便以后回顾。
import time
def demo_1(self):
# 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
nums = [1, 2, 3, 4]
count = 0
for i in range(len(nums)):
for j in range(len(nums)):
for k in range(len(nums)):
if nums[i] != nums[j] and nums[j] != nums[k] and nums[i] != nums[k]:
count += 1
print(nums[i], nums[j], nums[k])
print('可组成{}组无重复的三位数'.format(count))
def demo_2(self):
# 企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;
# 利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%;
# 20万到40万之间时,高于20万元的部分,可提成5%;
# 40万到60万之间时高于40万元的部分,可提成3%;
# 60万到100万之间时,高于60万元的部分,可提成1.5%,
# 高于100万元时,超过100万元的部分按1%提成,
# 从键盘输入当月利润I,求应发放奖金总数?
money = 0
i = float(input('请输入利润(I)万元:'))
i_list = [0, 10, 20, 40, 60, 100]
rate = [0.1, 0.075, 0.05, 0.03, 0.015, 0.01]
# 倒序访问列表
for x in range(len(i_list), -1, -1):
if i > i_list[x - 1]:
# 计算超出部分的奖金
money += (i - i_list[x - 1]) * rate[x - 1]
# 重置利润
i = i_list[x - 1]
print('奖金为:{}万元'.format(money))
def demo_3(self):
# 将一个列表的数据复制到另一个列表中。
list_a = [1, 2, 5, 11, 4]
list_b = list_a.copy()
print(list_b)
def demo_4(self):
# 输入某年某月某日,判断这一天是这一年的第几天?
year = int(input('请输入年'))
month = int(input('请输入月'))
day = int(input('请输入日'))
# 每月天数
days = [28, 31, 30, 31, 30, 31, 31, 30, 31, 30]
# 判断是否是闰年(四年一闰,百年不闰,四百年再闰)
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
days[0] = 29
if month != 1:
for i in range(month - 1):
day += days[i]
print('这是当年的第{}天'.format(day))
def demo_5(self):
# 输入三个整数x,y,z,请把这三个数由小到大输出。
x = 3
y = 2
z = 1
if x > y:
# x、y 值互换
x, y = y, x
if y > z:
y, z = z, y
if x > y:
x, y = y, x
print(x, y, z)
def demo_6(self):
# 判断101-200之间有多少个素数,并输出所有素数。
# 素数又叫质数。素数,指的是“大于1的整数中,只能被1和这个数本身整除的数,偶数只有2是素数故这里可以排除偶数
count = 0
for i in range(101, 201, 2):
flag = True
for j in range(2, i):
if i % j == 0:
flag = False
break
if flag:
count += 1
print("{}是素数".format(i))
print('一共有{}个素数'.format(count))
def demo_7(self):
# 打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。
# '//' 表示相除后并向下取整
for i in range(100, 1000):
x = i // 100
y = (i % 100) // 10
z = i % 10
if x**3 + y**3 + z**3 == i:
print('{}是水仙花数'.format(i))
def demo_8(self):
# 暂停一秒输出,并格式化当前时间。
for i in range(1, 11):
now_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(now_time)
time.sleep(1)
def demo_9(self):
# 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
# [1, 1, 2, 3, 5, 8, 13, 21, 34, 55] 每个数是前两个数之和
month = 10
a = 1
b = 1
num = [1, 1]
for i in range(month - 2):
a, b = b, a + b
num.append(b)
print('{}月后兔子数量为{}'.format(i + 3, b))
def demo_10(self):
# 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
score = [90, 40, 80, 20, 99, 70, 60, 89]
grade = []
for i in score:
if i >= 90:
grade.append('A')
elif 60 <= i <= 89:
grade.append('B')
else:
grade.append('C')
# 拼接成字典
result = dict(zip(score, grade))
print(result)
def demo_11(self):
# 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
s = '2023-1-13 小python跟你说:good morning!'
alphabet = 0
num = 0
blank = 0
other = 0
for i in range(len(s)):
if s[i].isdigit():
num += 1
elif 'a' <= s[i] <= 'z' or 'A' <= s[i] <= 'Z':
# s[i].isalpha()可判断字母但无法区分文字和字母
alphabet += 1
elif s[i].isspace():
blank += 1
else:
other += 1
print('字母{}个,数字{}个,空格{}个,其他字符{}个'.format(alphabet, num, blank, other))
def demo_12(self):
# 求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。
a = input('请输入数字a:')
n = int(input('请输入相加次数:'))
s = 0
formual = ''
for i in range(1, n + 1):
s += int(a * i)
formual += a * i + '+'
# strip用于删除字符串头尾指定字符
print('{}={}'.format(formual.strip('+'), s))
pass
def demo_13(self):
# 一球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
m = 100
sum_m = m
for i in range(1, 11):
sum_m += m
m = m / 2
print('第{}次反弹,经过距离:{},反弹高度:{}'.format(i, sum_m, m))
def demo_14(self):
# 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
# 第二天早上又将剩下的桃子吃掉一半,又多吃了一个
# 以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。1534
num = 1
for i in range(9):
num = (num + 1) * 2
print('一共摘了{}个桃子'.format(num))
def demo_15(self):
# 打印出如下图案(菱形):
# *
# ***
# *****
# *******
# *****
# ***
# *
# 方案一
"""num = 4
for i in range(1, 8, 2):
num -= 1
s = ' ' * num + '*' * i
print(s)
for i in range(5, 0, -2):
num += 1
s = ' ' * num + '*' * i
print(s)"""
# 方案二
for i in range(7):
if i < 4:
s = ' ' * (3 - i) + '*' * (2 * i + 1)
print(s)
else:
s = ' ' * (i - 3) + '*' * (2 * (7 - i) - 1)
print(s)
文章主要参考自:https://blog.csdn.net/blackcoffee_kou/article/details/122097060 ,这位大大写了100例,我就只挑选了几个我比较喜欢的,解法也略有不同