def main():
value = input("请输入长方体的长、宽、高(空格分隔):")
length, width, height = [eval(i) for i in value.split(" ")]
volume = length * width * height
print("长方体的体积为%.2f" % volume)
main()
def f(n):
if n == 1 or n == 2:
return 1
return f(n - 1) + f(n - 2)
def main():
num = int(input("请输入斐波那契数列的阶数:"))
print("斐波那契数列中小于等于参数{}的所有值:".format(num), end="")
for i in range(num - 1):
print(f(i + 1), end=",")
print(f(num))
main()
def main():
num = int(input("请输入一个整数:"))
total = 0
for i in range(1, int(num / 2) + 1):
if num % i == 0:
total += i
if total == num:
print("%d是一个完全数" % num)
else:
print("%d不是一个完全数" % num)
main()
def H(n):
if 0 <= n < 3:
return n
else:
return H(n - 1) + 9 * H(n - 2) - 9 * H(n - 3)
def main():
num = int(input("请输入参数n:"))
print("函数H(n)小于等于参数n的所有值为:")
for i in range(num):
print(H(i), end=',')
print(H(num))
main()
def B_search(array, target):
array.sort()
low = 0
height = len(array) - 1
while low <= height:
mid = (low + height) // 2
if target == array[mid]:
return mid
elif target < array[mid]:
height = mid - 1
else:
low = mid + 1
return False
def main():
my_array = [1, 3, 5, 7, 9, 11, 13]
num = eval(input("请输入要查找的数:"))
result = B_search(my_array, num)
if result:
print("要查找的数在数组中的下标为:", result)
else:
print("数组中不存在要查找的数")
main()
def main():
x = input("请输入参数x:")
n = eval(input("请输入参数n:"))
exp = x
for i in range(2, n + 1):
exp += ' + ' + x * i
print(exp + " =", eval(exp))
main()
def main():
n = eval(input("请输入参数n:"))
exp = "1"
for i in range(2, n + 1):
if i % 2 == 0:
exp += " + "
else:
exp += " - "
exp += "1/" + str(i)
print(exp + " =", round(eval(exp), 5))
main()
def main():
s = input("请输入一串字符串:")
is_palindrome = lambda string: string == string[::-1]
if is_palindrome(s):
print(s + "是回文")
else:
print(s + "不是回文")
main()
import re
def main():
s = input("请输入一串字符串:")
punctuation = '!,;:?"\'、,.'
s = re.sub(r'[{}]+'.format(punctuation), '', s)
s_inverse = s[::-1]
print("颠倒后的字符串为:", s_inverse)
main()
def main():
price = 54 + 53
money = [20, 10, 2, 1]
num = 0
for m in money:
if price >= m:
num += price // m
print("{}张{}元".format(price // m, m))
price %= m
else:
continue
print("最少需要{}张货币".format(num))
main()
本周的题目类型基本都是函数相关的,虽然题目的难度属于初中级,但是亲自做的时候还是感到不是特别流畅,对python中的许多语法还是无法做到自如地运用。比如lamda函数的使用实际上是可以更灵活的,但我自身只了解一些基础的用法,使用得很牵强,之后应多去了解一下各种语法的使用技巧。