上海网站建设助君网络7提交百度一下
你好,我是悦创。
待会更新~
更新计划
- 答案
- 题目
- 记得点赞收藏!
题目
之后更新
Solution
Question 1
# 读取输入
a = float(input("请输入实数 a: "))
b = float(input("请输入实数 b: "))
c = float(input("请输入实数 c: "))# 计算和
sum = a + b + c# 输出结果,保留两位小数
print(f"{sum:.2f}")
Question 2
class Add:def __init__(self):self.value = Nonedef add(self, a, b):self.value = a + bclass AddInt(Add):def add(self, a, b):if isinstance(a, int) and isinstance(b, int):self.value = a + bclass AddN(Add):def __init__(self, n):super().__init__()self.n = ndef add(self, a):self.value = self.n + aa = Add()
a.add(2023, 1223)
print(a.value)a = AddInt()
a.add(2023, 12.23)
print(a.value is None)a = AddN(-1)
a.add(1)
print(a.value)
Question 3
class Vector:def __init__(self, values):# 如果values是单一的数字,将其转换为长度为1的列表if isinstance(values, (int, float)):values = [values]self.values = valuesdef __str__(self):return str(self.values)def __len__(self):return len(self.values)def __add__(self, other):# 处理与实数的加法,将实数转换为长度为1的向量if isinstance(other, (int, float)):if len(self) != 1:raise ValueError("Cannot add scalar to Vector of length other than 1.")return Vector([self.values[0] + other])if isinstance(other, Vector):if len(self) != len(other):raise ValueError("Vectors are of different lengths.")return Vector([a + b for a, b in zip(self.values, other.values)])raise ValueError("Can only add Vector or scalar to Vector.")def __radd__(self, other):return self.__add__(other)# 用于判题程序的交互部分
try:eval(input())
except ValueError:eval(input())# print(len(Vector([])))
# print(5 + Vector([1, 2, 3, 4]))
# print("HelloWorld")
Question 4
def two_numbers_star(x, y):if x == 0 or y == 0:return 0if x % 2 == 0 and y % 2 == 0:return 2 * two_numbers_star(x // 2, y // 2)elif x % 2 == 1 and y % 2 == 1:return 2 * two_numbers_star((x - 1) // 2, (y - 1) // 2)elif x % 2 == 0 and y % 2 == 1:return 1 + 2 * two_numbers_star(x // 2, (y - 1) // 2)else:return two_numbers_star(y, x)# 测试样例
x = 12
y = 23
result = two_numbers_star(x, y)
result
Question 5
def find_minimal_sum_revised(n, numbers):total_sum = sum(numbers)index_dict = {}min_value = float('inf')for i in range(n):pair_num = total_sum - numbers[i]if pair_num in index_dict and index_dict[pair_num] != i:current_value = i + 1 + (index_dict[pair_num] + 1) * (n + 1)min_value = min(min_value, current_value)if numbers[i] not in index_dict:index_dict[numbers[i]] = ireturn min_value if min_value != float('inf') else 0n = 5
numbers = [15, -11, 2, 7, -4]
find_minimal_sum_revised(n, numbers)