【发布时间】:2021-09-21 03:21:07
【问题描述】:
代码应该在 price_t
测试用例
7 50 \n
1 12 5 111 200 1000 10
first_multiple_input = input().rstrip().split()
n = int(first_multiple_input[0])
k = int(first_multiple_input[1])
prices = list(map(int, input().rstrip().split()))
prices=sorted(prices)
price_t=0
count=0
while price_t <= k:
for i in range(len(prices)):
price_t=price_t+prices[i]
count+=1
print(count )
【问题讨论】:
-
如果条件被破坏,你应该终止 for 循环。
-
在外循环再次检查条件之前,您的内循环将完整运行。然后,如果条件不满足,它将再次运行内循环完全通过,然后再次检查条件
-
可能您想迭代
for price in prices并在循环内累积每个连续的price仅if price_t <= k并在达到阈值后跳过其余部分。 -
@G.Anderson 非常感谢您!你的回答正是我需要知道的。
标签: python while-loop