买古董
小明在古董市场上用手中的m元买东西,古董编号从0-10^9,编号=价格,小明手里已经有n件,小明不会重复购买,每件物品最多持有一个。帮小明计算手里的m元还能购买多少件?
输入 :
第一行:小明已经拥有的股东数目n和手上的金额m
第二行:小明已经拥有的古董编号,每个编号以空格隔开,总共n个编号。
输出:小明以手里的m元还能买多少件古董。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| def calculate_purchasable_antiques(n, m, owned_ids): owned_set = set(owned_ids) purchasable_count = 0 current_price = 0
while m >= current_price and current_price <= 10**9: if current_price not in owned_set: if m >= current_price: m -= current_price purchasable_count += 1 else: break current_price += 1
return purchasable_count
n, m = map(int, input().split()) owned_ids = list(map(int, input().split()))
result = calculate_purchasable_antiques(n, m, owned_ids) print(result)
|
吃桃子的最小重量
这个题意不是非常明确。我没看明白题目意思。
小明在桌子上放了一排桃子,吃掉其中一个时,会同时吃掉相邻两个,否则宁可不吃。桃子重量不相同。怎么样才能使吃到的桃子总重量最少。
输入:
第一行,一个整数n,表示桃子总数
第二行,一个整数数组,表示桃子的不同重量
输出:一个整数,表示所吃桃子的最小总重量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| def minWeight(n, weights): if n == 0: return 0 if n == 1: return weights[0] dp = [[0] * (n + 1) for _ in range(n + 1)] for length in range(3, n + 1): for i in range(1, n - length + 2): j = i + length - 1 dp[i][j] = float('inf') for k in range(i, j - 1): dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 2][j] + weights[i - 1] + weights[k] + weights[j]) return dp[1][n]
n = int(input().strip()) weights = list(map(int, input().strip().split()))
print(minWeight(n, weights))
|
或者 GPT 的答案:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| def min_weight_to_eat(n, weights): if n < 3: return 0
total_weight = 0 eaten = [False] * n
for i in range(1, n - 1): if not eaten[i - 1] and not eaten[i] and not eaten[i + 1]: total_weight += weights[i] eaten[i - 1] = eaten[i] = eaten[i + 1] = True
return total_weight
n = int(input()) weights = list(map(int, input().split()))
result = min_weight_to_eat(n, weights) print(result)
|