可乐瓶:
def exchange(num):
re=0
while num//3:
re=re+num//3
num=num//3+num%3
if num == 2:
re=re+1
return re
num=int(input())
print(exchange(num))
多功能列表:
现有一列表 ls = ['the lord of the rings','anaconda','legally blonde','gone with the wind'],编写程序,实现以下功能:
1. 输入“1”,输出元素为0-9的3次方的列表
2. 输入“2”,输出元素为0-9中偶数的3次方的列表
3. 输入“3”,输出元素为元组的列表,元组中元素依次是0-9中的奇数和该数的3次方
4. 输入“4”,将列表 ls 中每个元素首字母转为大写字母,输出新列表
5. 输入其他字符,输出“End of the program”
n=eval(input())
if n==1:
s=list(map(lambda x: x**3,range(0,10)))
print(s)
elif n==2:
l=[]
for i in range(0,10):
if i%2==0:
l.append(i)
s=list(map(lambda x: x**3,l))
print(s)
elif n==3:
l=[]
for i in range(0,10):
if i%2==1:
p=(i,i**3)
l.append(p)
print(l)
elif n==4:
l=['the lord of the rings','anaconda','legally blonde','gone with the wind']
s=list(map(lambda x:x.capitalize(),l))
print(s)
else
print("End of the program")
杨辉三角形:
def fun(n):
# 生成n行的杨辉三角列表
ls = [[1], [1, 1]]
if n <= 2:
return ls[:n]
else:
for i in range(3, n + 1):
# 增加新的一行,该行开头第一个元素为1
ls.append([1])
# 由旧一行,定义生成新一行的元素
for j in range(i - 2):
ls[-1].append(ls[-2][j] + ls[-2][j + 1])
# 新一行添加最后一个元素1
ls[-1].append(1)
return ls
n = int(input())
ls = fun(n)
wide = len(str(max(ls[-1])))
# en凑输出格式
for i in range(n):
print(end=' ' * ((n - i - 1) * wide))
for j in ls[i]:
# 字符串右对齐,不足补空
s = str(j).rjust(wide * 2)
print(s, end='')
print(end='\n')